0

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? :(

1
  • btw how do i format markup on stackexchange??
    – roman p.
    Oct 27 '20 at 19:19
0

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

1
  • oh dear, i just grep'ed for page.main.title and didn't realised it shows up in soo many places :O i initially just grep'ed for htmlClass="page-title-wrapper" and only saw title.phtml referenced from that thank you
    – roman p.
    Oct 27 '20 at 21:40
0

Try this

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

Happy coding

1
  • I've tried and but got an exception with a huge stack trace :? magento is hard lol. thank you
    – roman p.
    Oct 27 '20 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.