Question

I'm struggling with something simple.

I'm trying to print the Page Title in the custom block from my custom.phtml and I'm getting nothing.

I've removed the Magento original page title block from all pages by updating default.xml

referenceBlock name="page.main.title" remove="true"

I've created my own block and pointed it to my phtml file in the same default.xml

block class="Magento\Framework\View\Element\Template" name="page_title" template="Magento_Theme::page_title.phtml"

Now the problem is even if i copy paste the contents of original /module-theme/view/frontend/templates/html/title.phtml

into my custom page_title.phtml I'm getting nothing, 0. It's not the case of caching or anything similar as any other type of content gets displayed no problem. :(

All I'm looking for is a way to access that $titleHtml variable from title.phtml somehow... Any help? :(

Was it helpful?

Solution

If you look for the string 'page.main.title' in vendor/magento folder, you will see that the title is set within the layout; for instance, below:

 <referenceBlock name="page.main.title">
                <action method="setPageTitle">
                    <argument translate="true" name="page_title" xsi:type="string">Compare Products</argument>
                </action>
            </referenceBlock>

So, since you have removed this reference, there is no instruction your block has to perform.

I have myself come up with something by using the below:

<block name="page.title" template="Magento_Theme::html/title.phtml">
            <arguments>
                <argument translate="true" name="page_heading" xsi:type="string">My Custom Title</argument>
            </arguments>
        </block>

If you want to make you content dynamic,then just add either a helper or a class and you will be able to extend this further

OTHER TIPS

Try this

echo $block->getLayout()->getBlock('page.main.title')->getPageTitle();

Happy coding

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top