Magento-template-hint

Why Should You Use Magento 2 Template Hints For Your Store?

Posted by

Magento 2 includes 3 structures to present your website to customers that are layout, block, and container.

The page structure looks the same as the XML (Extensible Markup Language) file that is much like the HTML (HyperText Markup Language) structure to create one or several columns.

Do you ever wonder how to identify each structure to design your website?

Magento 2 Template Hints are a useful tool to help you identify each block added to each page. Moreover, if you want to make a change, you need to know where that block is generated.

Let’s go to check with us how to enable this Magento Template hints!

Why Should You Use Magento 2 Template Hints For Your Store?

Magento-2-Template-Hints

>>> Don’t miss this: The Best Magento Tutorials for eCommerce Store in 2020!

Debug is a crucial part of processing development in building an eCommerce store. The developer always knows what they should do when creating a website to ensure that it runs smoothly.

Magento 2 Template Hints is the part of this Debug process. It is a debugging tool that applies notions with a path on each template of the Magento 2 platform.

This mode displays the errors directly on the screen. In fact, it is a convenient way to debug even failed modules that show a blank page or 500 errors on the screen.

Here is the default Magento homepage with the images and banners installed.

Magento-2-Template-Hints

And here is the thing you see at the backend store.

Magento-2-Template-Hints

>>> Read more: Magento 2 MFTF to improve your eCommerce Store!

You can also check out Xdebug combination with PHP to help you with debugging and developing the website.

It helps you to establish breakpoints in your code and interrupt the PHP interpreter for displaying the application data on each executed line at such breakpoints.

So, do you know how to enable the Template Path of Magento? Let’s scroll down to check how to turn it on to help you manage your site faster.

How To Enable Magento Template Path Hints?

#1. Enabling Magento 2 Template Path Hints via Admin Panel

First, you log in to your Magento 2 account and go to Dashboard -> Stores -> Configuration.

Magento-2-Template-Hints

Secondly, in tab Advanced, you click Developer.

Magento-2-Template-Hints

>>> Don’t miss this: Elasticsearch Magento 2 to support your store performing!
  • Allowed IPs: fills your IP.
  • Template Path Hints: choose Yes
  • Add Block Names to Hints: choose Yes

Magento-2-Template-Hints

After doing all the steps, your website will look like this in the storefront:

Magento-2-Template-Hints

#2. Enabling Magento 2 Template Hints via Command Line

At first, you have to connect your Magento 2 store with SSH terminal and then go to your Magento 2 store file.

Enable Magento template path hints for frontend:

<?php bin/magento dev:template-hints:enable

Disable Magento template hints for frontend:

<?php bin/magento dev:template-hints:enable

Clean and flush cache:

<?bin/magento cache:clean
bin/magento cache:flush

However, you cannot enable or disable Magento 2 Template Hints for the Admin if you use this method.

#3. Enabling Magento 2 Template Hints via Source Code

  • Opening your file following the address below:

Vendor\magento\module-developer\Model\TemplateEngine\Plugin\DebugHints.php

  • Then, you copy this code on your file:
<?public function afterCreate(

        TemplateEngineFactory $subject,

        TemplateEngineInterface $invocationResult

    ) {

        $storeCode = $this->storeManager->getStore()->getCode();

        // if ($this->scopeConfig->getValue($this->debugHintsPath, ScopeInterface::SCOPE_STORE, $storeCode)

        //     && $this->devHelper->isDevAllowed()) {

            $showBlockHints = $this->scopeConfig->getValue(

                self::XML_PATH_DEBUG_TEMPLATE_HINTS_BLOCKS,

                ScopeInterface::SCOPE_STORE,

                $storeCode

            );

            return $this->debugHintsFactory->create([

                'subject' => $invocationResult,

                'showBlockHints' => $showBlockHints,

            ]);

        //}

        return $invocationResult;

    }

#4. Enabling Magento 2 Template Hints via MySQL

  • First, you log into your Magento account and go to System > Configuration > Developer page, switch to Website view
  • Next, you can copy this code to enable the template:
<?SET @template_hints = 1;

INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('websites','1','dev/debug/template_hints', @template_hints) ON DUPLICATE KEY UPDATE `value`=@template_hints;

-- Enable block hints

SET @template_hints_blocks = 1;

INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('websites','1','dev/debug/template_hints_blocks', @template_hints_blocks) ON DUPLICATE KEY UPDATE `value`=@template_hints_blocks;
  • If you want to disable Magento 2 Template Hints, you can copy and paste the code below:
<?SET @template_hints = 0;

INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('websites','1','dev/debug/template_hints', @template_hints) ON DUPLICATE KEY UPDATE `value`=@template_hints;

-- Enable block hints

SET @template_hints_blocks = 0;

INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('websites','1','dev/debug/template_hints_blocks', @template_hints_blocks) ON DUPLICATE KEY UPDATE `value`=@template_hints_blocks;

However, there are many developers/users after following all steps that they still cannot enable from magento2 back-end section, it’s not showing.

Magento-2-Template-Hints

>>> Interested? Check more TOP 6 Magento 2 Performance Tips to rocket your store!

We’re going to help you solve the problems after this part.

#5. Enabling Template Path Hints via env.php file

Magento-2-Template-Hints

If you cannot change the value through the Admin Panel, you can search for the value on app/etc/env.php

In this file, you can check template_hints_storefront and template_hints_admin to change the value.

<?<?php

return array(

...

'system' =>

array(

'default' =>

array(

'dev' =>

array(

'debug' =>

array(

'template_hints_storefront' => '1',

'template_hints_admin' => '1'

),

),

),

),

);

Run the following command after the changes:

php bin/magento setup:upgrade

How To Fix Magento 2 Template Hints Not Showing?

Magento-2-Template-Hints

Firstly, you go to Dashboard -> Stores -> Configuration -> Advanced -> Developer.

And, next, you need to set Main Website in your Default store view.

Then, you can check your Configuration again whether Magento Template Hints working or not.

If still, your Magento 2 Template Hints not showing, you can open vendor/magento/module-developer/Model/TemplateEngine/Plugin/DebugHints.php

write this code inside afterCreate function : (at the start, above the storecode line)

<?if(isset($_GET['shreyasPathHints']) && $_GET['shreyasPathHints'] == 'on'){ return $this->debugHintsFactory->create([ 'subject' => $invocationResult, 'showBlockHints' => 1, ]); }

Now, you can open any Magento page and append

??shreyasPathHints=on

to the URL.

Conclusion

That is the way how to deal with Magento Template Path Hints for your store. In this article, I’ve shown you the benefits of Magento 2 Template Path Hints, how to enable and disable it, and how to solve if you cannot enable the Template Hints Path.

Again, Magento Template Path Hints is very important for your store because it shows the errors for you on the backend and helps you save your time to fix errors.

If you have any questions, you can comment on the comment section below.

We’re 24/7 support you!

Thank you for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *