I want to override product main page title of product view page with block class.

So I use referenceBlock page.main.title with class like this

<referenceBlock class="Magento\Catalog\Block\Product\View" name="page.main.title" template="Vendor_Module::html/title.phtml"/>

But Magento 2.2.1 shows error like this: not allow attrbute class with referenceBlock

So how can i achieve this in Magento 2.2.1?


Use this :

<referenceBlock name="page.main.title">
    <action method="setTemplate">
        <argument name="template" xsi:type="string">Vendor_Module::html/title.phtml</argument>

To override class using di.xml :

<preference for="Magento\Catalog\Block\Product\View" type="Vendor\ModuleName\Block\Product\View" />

Hope it Helps!

  • Thanks for answer. But I also want to change block class of referanceBlock. How can I change both block class and template with reference block? – Rahul Dec 13 '17 at 8:39
  • I know that how override block with di.xml. But I want just override template with custom block class with referenceBlock. – Rahul Dec 13 '17 at 9:51
  • Then you have to create new block and remove old one. – Ronak Chauhan Dec 13 '17 at 10:12

Use this:

<referenceBlock name="page.main.title">
    <block class="Magento\Catalog\Block\Product\View" name="abc" template="Vendor_Module::html/title.phtml"/>
  • 1
    Thanks for answer. But this will add new block in page.main.title . I need to override title.phtml with block class – Rahul Dec 13 '17 at 7:00

I found the solution

I just keep same name of block ad change both class and template.


<block class="Vendor\Module\Block\Product\View" name="page.main.title" template="Vendor_Module::html/title.phtml" before="product.info.main"/>

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.