magento2: XML syntax to change block's template
Summary
Documentation claims that it is possible to change a block’s template by the following layout update:
<referenceBlock name="navigation.sections">
<arguments>
<argument name="template" xsi:type="string">Magento_Theme::html/nav_sections.phtml</argument>
</arguments>
</referenceBlock>
But It doesn’t work.
Using action
node, instead of arguments
node, works fine:
<referenceBlock name="navigation.sections">
<action method="setTemplate">
<argument name="template" xsi:type="string">Magento_Theme::html/nav_sections.phtml</argument>
</action>
</referenceBlock>
Source: http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/xml-manage.html#set_template
Preconditions (*)
- Magento 2.3-develop
- PHP 7.2
Steps to reproduce (*)
Try to change a block’s template by the following layout update
e.g.:
Expected result (*)
Block’s template is changed without any errors
Actual result (*)
Got exception
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 18
- Comments: 21 (9 by maintainers)
Commits related to this issue
- magento/magento2#3356: XML syntax to change block's template — committed to nsergo/magento2 by nsergo 7 years ago
- Merge pull request #3356 from magento-borg/BugFixPR [2.3.0-regression] Bug Fixes — committed to magento-research/magento2 by joanhe 6 years ago
The reason why it doesn’t work is because the method \Magento\Framework\View\Layout\Generator\Block::generateBlock:
When we want to overwrite an existing block, this one was at first initialized via a layout XML with an attribute
template
.Into the
generateBlock
method,arguments
are passed to the block, thensetTemplate
is called and then in the calling methodprocess
the action is called.So the priority is the following:
template
When a block was initialized with its attribute
template
,arguments
will have no effect. That’s why you need to useactions
@magento-engcom-team the PR you’ve referencing was also closed, as it required some changes in the pull request code. So now both this issue and PR are closed but the issue still exists and documentation is still wrong. In my opinion this issue should stay open until it’s fixed.
If you consider this a feature request, please at last fix the documentation.
This issue still exist.
<referenceBlock name="product.info.addtocart"> <arguments> <argument name="template" xsi:type="string">Yoma_Dipen::addtocart.phtml</argument> </arguments> </referenceBlock>I tried this code in my customer module’s catalog_product_view.xml
it’s not working.
Closing the issue as the feature request. Please, see more details here https://github.com/magento/magento2/pull/8913#discussion_r106549969
This issue still exists. We should reopen it.
Doesn’t work in 2.3.3-p1
FYI This issue was resolved by adjusting the docs and highlighting the applied priorities in https://github.com/magento/devdocs/pull/7977
In 2.1.0 I still have to use ‘action’ and the online documentation is still wrong.