Magento 2: Changing a Block's Template The 2019 Stack Overflow Developer Survey Results Are Inhow to setTemplate for top.search in magento2Change block's template in layout XMLAdd custom copyright text below existing copyright text in Magento 2 frontend by moduleProblem overrding .phtml file: Magento 2 Catalog modulereplace or over write the Magento 2 footer phtml filePlease correct the XML data and try again with simple settemplateHow to switch off cache in referenceBlockOverwrite template without the “name” attributeMagento 2.0.7 - Understanding the different block classes?how do overriding Block with template has changed?Changing header.phtml for an extensionShow block and template from other extensionMagento custom block shows Fatal error: Call to a member function setTemplate() on a non-objectStruggling with capital letters and xlm namespace while creating a block for a new moduleMagento xml layout for specific URLhow to call another plugin block method in custom template file in magentoCalling block's method in template doesn't workMagento2 Override Block - Invalid template file problemError in getting block in a templateMagento2 render custom block in adminhtml layout.xml
Why do UK politicians seemingly ignore opinion polls on Brexit?
What does "rabbited" mean/imply in this sentence?
Deadlock Graph and Interpretation, solution to avoid
If a poisoned arrow's piercing damage is reduced to 0, do you still get poisoned?
What do the Banks children have against barley water?
Pristine Bit Checking
The difference between dialogue marks
Inline version of a function returns different value then non-inline version
I looked up a future colleague on linkedin before I started a job. I told my colleague about it and he seemed surprised. Should I apologize?
It's possible to achieve negative score?
Could JWST stay at L2 "forever"?
"What time...?" or "At what time...?" - what is more grammatically correct?
Does it makes sense to buy a new cycle to learn riding?
How can I fix this gap between bookcases I made?
If the Wish spell is used to duplicate the effect of Simulacrum, are existing duplicates destroyed?
Where does the "burst of radiance" from Holy Weapon originate?
Inflated grade on resume at previous job, might former employer tell new employer?
Does duplicating a spell with Wish count as casting that spell?
Unbreakable Formation vs. Cry of the Carnarium
A poker game description that does not feel gimmicky
Realistic Alternatives to Dust: What Else Could Feed a Plankton Bloom?
Why don't Unix/Linux systems traverse through directories until they find the required version of a linked library?
How to reverse every other sublist of a list?
What are the motivations for publishing new editions of an existing textbook, beyond new discoveries in a field?
Magento 2: Changing a Block's Template
The 2019 Stack Overflow Developer Survey Results Are Inhow to setTemplate for top.search in magento2Change block's template in layout XMLAdd custom copyright text below existing copyright text in Magento 2 frontend by moduleProblem overrding .phtml file: Magento 2 Catalog modulereplace or over write the Magento 2 footer phtml filePlease correct the XML data and try again with simple settemplateHow to switch off cache in referenceBlockOverwrite template without the “name” attributeMagento 2.0.7 - Understanding the different block classes?how do overriding Block with template has changed?Changing header.phtml for an extensionShow block and template from other extensionMagento custom block shows Fatal error: Call to a member function setTemplate() on a non-objectStruggling with capital letters and xlm namespace while creating a block for a new moduleMagento xml layout for specific URLhow to call another plugin block method in custom template file in magentoCalling block's method in template doesn't workMagento2 Override Block - Invalid template file problemError in getting block in a templateMagento2 render custom block in adminhtml layout.xml
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
In Magento 1, as a module developer it's possible to change a block's template using layout XML code something like this
<reference name="block_to_change">
<action method="setTemplate">
<param>/path/to/template.phtml</param>
</action>
</reference>
and then adding your template to the base theme.
app/design/frontend/base/default/template/path/to/template.phtml
Is it possible, as a module developer, to do something similar in Magento 2? Or would I need to use layout XML or PHP code to remove the block I'm interested in, and insert a new block with a different template (whose class extends the original block class's)
I know I could create a custom theme that replaces a template, but I'm interested in creating a module that changes the default template, but still allows a custom theme to then replace that template.
layout blocks magento2 phtml
add a comment |
In Magento 1, as a module developer it's possible to change a block's template using layout XML code something like this
<reference name="block_to_change">
<action method="setTemplate">
<param>/path/to/template.phtml</param>
</action>
</reference>
and then adding your template to the base theme.
app/design/frontend/base/default/template/path/to/template.phtml
Is it possible, as a module developer, to do something similar in Magento 2? Or would I need to use layout XML or PHP code to remove the block I'm interested in, and insert a new block with a different template (whose class extends the original block class's)
I know I could create a custom theme that replaces a template, but I'm interested in creating a module that changes the default template, but still allows a custom theme to then replace that template.
layout blocks magento2 phtml
add a comment |
In Magento 1, as a module developer it's possible to change a block's template using layout XML code something like this
<reference name="block_to_change">
<action method="setTemplate">
<param>/path/to/template.phtml</param>
</action>
</reference>
and then adding your template to the base theme.
app/design/frontend/base/default/template/path/to/template.phtml
Is it possible, as a module developer, to do something similar in Magento 2? Or would I need to use layout XML or PHP code to remove the block I'm interested in, and insert a new block with a different template (whose class extends the original block class's)
I know I could create a custom theme that replaces a template, but I'm interested in creating a module that changes the default template, but still allows a custom theme to then replace that template.
layout blocks magento2 phtml
In Magento 1, as a module developer it's possible to change a block's template using layout XML code something like this
<reference name="block_to_change">
<action method="setTemplate">
<param>/path/to/template.phtml</param>
</action>
</reference>
and then adding your template to the base theme.
app/design/frontend/base/default/template/path/to/template.phtml
Is it possible, as a module developer, to do something similar in Magento 2? Or would I need to use layout XML or PHP code to remove the block I'm interested in, and insert a new block with a different template (whose class extends the original block class's)
I know I could create a custom theme that replaces a template, but I'm interested in creating a module that changes the default template, but still allows a custom theme to then replace that template.
layout blocks magento2 phtml
layout blocks magento2 phtml
edited Oct 12 '15 at 20:32
Alan Storm
asked Oct 12 '15 at 15:45
Alan StormAlan Storm
29.2k21119309
29.2k21119309
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
Of course, it is possible:
<referenceBlock name="copyright">
<action method="setTemplate">
<argument name="template" xsi:type="string">Dfr_Backend::page/copyright.phtml</argument>
</action>
</referenceBlock>
Can you explain the steps how can i change the layout, Actually i want to update the add to addtocart.phtml file according to system configuration and also want to update this using custom module
– Deepak Mankotia
Nov 25 '15 at 7:44
5
KAndy solution didn't work for me but this one yes
– csmarvz
Jan 26 '16 at 16:09
I have changed the template of the block name "customer_account_dashboard_top"<body> <referenceBlock name="customer_account_dashboard_top"> <action method="setTemplate"> <argument name="template" xsi:type="string">Namespace_Modulename::order/recentorder.phtml</argument> </action> </referenceBlock> </body>" but its not getting work, kindly check and let me know your comments
– senthil
Apr 26 '18 at 17:36
add a comment |
Action node is deprecated, but you can use block arguments
<referenceBlock name="block_to_change">
<arguments>
<argument name="template" xsi:type="string">[Vendor]_[Module]::/path/to/template.phtml</argument>
</arguments>
</referenceBlock>
Can you explain the steps how can i change the layout, Actually i want to update the add toaddtocart.phtml
file according to system configuration and also want to update this using custom module
– Deepak Mankotia
Nov 25 '15 at 7:44
1
see github.com/magento/magento2/blob/develop/dev/tests/static/…
– KAndy
Mar 9 '16 at 13:49
4
Thanks - I'll just leave a reference to a bug report here github.com/magento/magento2/issues/3356 - the method posted in this answer, while possibly the future way of doing things, does not yet work as advertised
– Kristof at Fooman
Mar 21 '16 at 10:27
2
@KAndy Is your code example 100% correct? I tried that and I can't get it to work in any way. The other answer from @Mage2.PRO (which uses<action method='setTemplate'>
) works without problems.
– maginfortis
May 24 '16 at 8:52
1
This doesn't work. Accepted answer does though.
– Milan Simek
Aug 20 '17 at 22:20
|
show 4 more comments
To understand the difference between <arguments>
and <action>
you must understand how the constructors of Magento 2 objects work. If you override a constructor in Magento, you'll always get a $data-parameter
which is an array. This is the data as provided in the XML files and translated to the internal $_data-array
of MagentoFrameworkDataObject
:
<referenceBlock name="catalog.topnav">
<arguments>
<argument name="template" xsi:type="string">Foo_Bar::buzz.phtml</argument>
</arguments>
</referenceBlock>
...
public function __construct(array $data = [])
// $_data is populated with the arguments from XML:
// so $_data['template'] is now 'Foo_Bar::buzz.phtml'
$this->_data = $data;
However, in the case of a template, if setTemplate()
is used in the pseudo constructor (_construct()
, single underscore), this means that the $data
is overridden, no matter if it's set in the XML.
public function _construct()
$this->setTemplate('foo/bar.phtml');
In that scenario, <action>
is prefered, since this is executed after the constructor & pseudo constructor.
<referenceBlock name="catalog.topnav">
<action method="setTemplate">
<argument name="template" xsi:type="string">Foo_Bar::buzz.phtml</argument>
</action>
</referenceBlock>
add a comment |
The following worked for me in Magento EE 2.2.3
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="core.module.block.name" template="[Vendor]_[Module]::path/to/your/template.phtml" />
</body>
</page>
Note: if you are using a custom module to change a core's template and you are going mad because the previous code snipped does not work, make sure your module is loaded after the core module you are trying to change (module.xml) and you executed bin/magento setup:upgrade
:)
This is the cleanest way in my opinion.
– Ben Crook
May 25 '18 at 16:02
add a comment |
I don't know why, but i find this way to be the best:
<referenceBlock name="sales.order.items.renderers.default" template="Foo_Bar::sales/order/items/renderer/default.phtml"/>
add a comment |
<referenceBlock name="sales.order.items.renderers.default" template="Foo_Bar::sales/order/items/renderer/default.phtml"/>
This will work only if your block wasn't overwritten before using setTemplate
method. Magento 2.2.x and higher.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f86188%2fmagento-2-changing-a-blocks-template%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Of course, it is possible:
<referenceBlock name="copyright">
<action method="setTemplate">
<argument name="template" xsi:type="string">Dfr_Backend::page/copyright.phtml</argument>
</action>
</referenceBlock>
Can you explain the steps how can i change the layout, Actually i want to update the add to addtocart.phtml file according to system configuration and also want to update this using custom module
– Deepak Mankotia
Nov 25 '15 at 7:44
5
KAndy solution didn't work for me but this one yes
– csmarvz
Jan 26 '16 at 16:09
I have changed the template of the block name "customer_account_dashboard_top"<body> <referenceBlock name="customer_account_dashboard_top"> <action method="setTemplate"> <argument name="template" xsi:type="string">Namespace_Modulename::order/recentorder.phtml</argument> </action> </referenceBlock> </body>" but its not getting work, kindly check and let me know your comments
– senthil
Apr 26 '18 at 17:36
add a comment |
Of course, it is possible:
<referenceBlock name="copyright">
<action method="setTemplate">
<argument name="template" xsi:type="string">Dfr_Backend::page/copyright.phtml</argument>
</action>
</referenceBlock>
Can you explain the steps how can i change the layout, Actually i want to update the add to addtocart.phtml file according to system configuration and also want to update this using custom module
– Deepak Mankotia
Nov 25 '15 at 7:44
5
KAndy solution didn't work for me but this one yes
– csmarvz
Jan 26 '16 at 16:09
I have changed the template of the block name "customer_account_dashboard_top"<body> <referenceBlock name="customer_account_dashboard_top"> <action method="setTemplate"> <argument name="template" xsi:type="string">Namespace_Modulename::order/recentorder.phtml</argument> </action> </referenceBlock> </body>" but its not getting work, kindly check and let me know your comments
– senthil
Apr 26 '18 at 17:36
add a comment |
Of course, it is possible:
<referenceBlock name="copyright">
<action method="setTemplate">
<argument name="template" xsi:type="string">Dfr_Backend::page/copyright.phtml</argument>
</action>
</referenceBlock>
Of course, it is possible:
<referenceBlock name="copyright">
<action method="setTemplate">
<argument name="template" xsi:type="string">Dfr_Backend::page/copyright.phtml</argument>
</action>
</referenceBlock>
edited Oct 10 '17 at 15:34
Roman Snitko
374212
374212
answered Oct 12 '15 at 15:52
Mage2.PROMage2.PRO
3,70111319
3,70111319
Can you explain the steps how can i change the layout, Actually i want to update the add to addtocart.phtml file according to system configuration and also want to update this using custom module
– Deepak Mankotia
Nov 25 '15 at 7:44
5
KAndy solution didn't work for me but this one yes
– csmarvz
Jan 26 '16 at 16:09
I have changed the template of the block name "customer_account_dashboard_top"<body> <referenceBlock name="customer_account_dashboard_top"> <action method="setTemplate"> <argument name="template" xsi:type="string">Namespace_Modulename::order/recentorder.phtml</argument> </action> </referenceBlock> </body>" but its not getting work, kindly check and let me know your comments
– senthil
Apr 26 '18 at 17:36
add a comment |
Can you explain the steps how can i change the layout, Actually i want to update the add to addtocart.phtml file according to system configuration and also want to update this using custom module
– Deepak Mankotia
Nov 25 '15 at 7:44
5
KAndy solution didn't work for me but this one yes
– csmarvz
Jan 26 '16 at 16:09
I have changed the template of the block name "customer_account_dashboard_top"<body> <referenceBlock name="customer_account_dashboard_top"> <action method="setTemplate"> <argument name="template" xsi:type="string">Namespace_Modulename::order/recentorder.phtml</argument> </action> </referenceBlock> </body>" but its not getting work, kindly check and let me know your comments
– senthil
Apr 26 '18 at 17:36
Can you explain the steps how can i change the layout, Actually i want to update the add to addtocart.phtml file according to system configuration and also want to update this using custom module
– Deepak Mankotia
Nov 25 '15 at 7:44
Can you explain the steps how can i change the layout, Actually i want to update the add to addtocart.phtml file according to system configuration and also want to update this using custom module
– Deepak Mankotia
Nov 25 '15 at 7:44
5
5
KAndy solution didn't work for me but this one yes
– csmarvz
Jan 26 '16 at 16:09
KAndy solution didn't work for me but this one yes
– csmarvz
Jan 26 '16 at 16:09
I have changed the template of the block name "customer_account_dashboard_top"<body> <referenceBlock name="customer_account_dashboard_top"> <action method="setTemplate"> <argument name="template" xsi:type="string">Namespace_Modulename::order/recentorder.phtml</argument> </action> </referenceBlock> </body>" but its not getting work, kindly check and let me know your comments
– senthil
Apr 26 '18 at 17:36
I have changed the template of the block name "customer_account_dashboard_top"<body> <referenceBlock name="customer_account_dashboard_top"> <action method="setTemplate"> <argument name="template" xsi:type="string">Namespace_Modulename::order/recentorder.phtml</argument> </action> </referenceBlock> </body>" but its not getting work, kindly check and let me know your comments
– senthil
Apr 26 '18 at 17:36
add a comment |
Action node is deprecated, but you can use block arguments
<referenceBlock name="block_to_change">
<arguments>
<argument name="template" xsi:type="string">[Vendor]_[Module]::/path/to/template.phtml</argument>
</arguments>
</referenceBlock>
Can you explain the steps how can i change the layout, Actually i want to update the add toaddtocart.phtml
file according to system configuration and also want to update this using custom module
– Deepak Mankotia
Nov 25 '15 at 7:44
1
see github.com/magento/magento2/blob/develop/dev/tests/static/…
– KAndy
Mar 9 '16 at 13:49
4
Thanks - I'll just leave a reference to a bug report here github.com/magento/magento2/issues/3356 - the method posted in this answer, while possibly the future way of doing things, does not yet work as advertised
– Kristof at Fooman
Mar 21 '16 at 10:27
2
@KAndy Is your code example 100% correct? I tried that and I can't get it to work in any way. The other answer from @Mage2.PRO (which uses<action method='setTemplate'>
) works without problems.
– maginfortis
May 24 '16 at 8:52
1
This doesn't work. Accepted answer does though.
– Milan Simek
Aug 20 '17 at 22:20
|
show 4 more comments
Action node is deprecated, but you can use block arguments
<referenceBlock name="block_to_change">
<arguments>
<argument name="template" xsi:type="string">[Vendor]_[Module]::/path/to/template.phtml</argument>
</arguments>
</referenceBlock>
Can you explain the steps how can i change the layout, Actually i want to update the add toaddtocart.phtml
file according to system configuration and also want to update this using custom module
– Deepak Mankotia
Nov 25 '15 at 7:44
1
see github.com/magento/magento2/blob/develop/dev/tests/static/…
– KAndy
Mar 9 '16 at 13:49
4
Thanks - I'll just leave a reference to a bug report here github.com/magento/magento2/issues/3356 - the method posted in this answer, while possibly the future way of doing things, does not yet work as advertised
– Kristof at Fooman
Mar 21 '16 at 10:27
2
@KAndy Is your code example 100% correct? I tried that and I can't get it to work in any way. The other answer from @Mage2.PRO (which uses<action method='setTemplate'>
) works without problems.
– maginfortis
May 24 '16 at 8:52
1
This doesn't work. Accepted answer does though.
– Milan Simek
Aug 20 '17 at 22:20
|
show 4 more comments
Action node is deprecated, but you can use block arguments
<referenceBlock name="block_to_change">
<arguments>
<argument name="template" xsi:type="string">[Vendor]_[Module]::/path/to/template.phtml</argument>
</arguments>
</referenceBlock>
Action node is deprecated, but you can use block arguments
<referenceBlock name="block_to_change">
<arguments>
<argument name="template" xsi:type="string">[Vendor]_[Module]::/path/to/template.phtml</argument>
</arguments>
</referenceBlock>
edited Oct 13 '15 at 13:07
answered Oct 12 '15 at 18:03
KAndyKAndy
16.1k23245
16.1k23245
Can you explain the steps how can i change the layout, Actually i want to update the add toaddtocart.phtml
file according to system configuration and also want to update this using custom module
– Deepak Mankotia
Nov 25 '15 at 7:44
1
see github.com/magento/magento2/blob/develop/dev/tests/static/…
– KAndy
Mar 9 '16 at 13:49
4
Thanks - I'll just leave a reference to a bug report here github.com/magento/magento2/issues/3356 - the method posted in this answer, while possibly the future way of doing things, does not yet work as advertised
– Kristof at Fooman
Mar 21 '16 at 10:27
2
@KAndy Is your code example 100% correct? I tried that and I can't get it to work in any way. The other answer from @Mage2.PRO (which uses<action method='setTemplate'>
) works without problems.
– maginfortis
May 24 '16 at 8:52
1
This doesn't work. Accepted answer does though.
– Milan Simek
Aug 20 '17 at 22:20
|
show 4 more comments
Can you explain the steps how can i change the layout, Actually i want to update the add toaddtocart.phtml
file according to system configuration and also want to update this using custom module
– Deepak Mankotia
Nov 25 '15 at 7:44
1
see github.com/magento/magento2/blob/develop/dev/tests/static/…
– KAndy
Mar 9 '16 at 13:49
4
Thanks - I'll just leave a reference to a bug report here github.com/magento/magento2/issues/3356 - the method posted in this answer, while possibly the future way of doing things, does not yet work as advertised
– Kristof at Fooman
Mar 21 '16 at 10:27
2
@KAndy Is your code example 100% correct? I tried that and I can't get it to work in any way. The other answer from @Mage2.PRO (which uses<action method='setTemplate'>
) works without problems.
– maginfortis
May 24 '16 at 8:52
1
This doesn't work. Accepted answer does though.
– Milan Simek
Aug 20 '17 at 22:20
Can you explain the steps how can i change the layout, Actually i want to update the add to
addtocart.phtml
file according to system configuration and also want to update this using custom module– Deepak Mankotia
Nov 25 '15 at 7:44
Can you explain the steps how can i change the layout, Actually i want to update the add to
addtocart.phtml
file according to system configuration and also want to update this using custom module– Deepak Mankotia
Nov 25 '15 at 7:44
1
1
see github.com/magento/magento2/blob/develop/dev/tests/static/…
– KAndy
Mar 9 '16 at 13:49
see github.com/magento/magento2/blob/develop/dev/tests/static/…
– KAndy
Mar 9 '16 at 13:49
4
4
Thanks - I'll just leave a reference to a bug report here github.com/magento/magento2/issues/3356 - the method posted in this answer, while possibly the future way of doing things, does not yet work as advertised
– Kristof at Fooman
Mar 21 '16 at 10:27
Thanks - I'll just leave a reference to a bug report here github.com/magento/magento2/issues/3356 - the method posted in this answer, while possibly the future way of doing things, does not yet work as advertised
– Kristof at Fooman
Mar 21 '16 at 10:27
2
2
@KAndy Is your code example 100% correct? I tried that and I can't get it to work in any way. The other answer from @Mage2.PRO (which uses
<action method='setTemplate'>
) works without problems.– maginfortis
May 24 '16 at 8:52
@KAndy Is your code example 100% correct? I tried that and I can't get it to work in any way. The other answer from @Mage2.PRO (which uses
<action method='setTemplate'>
) works without problems.– maginfortis
May 24 '16 at 8:52
1
1
This doesn't work. Accepted answer does though.
– Milan Simek
Aug 20 '17 at 22:20
This doesn't work. Accepted answer does though.
– Milan Simek
Aug 20 '17 at 22:20
|
show 4 more comments
To understand the difference between <arguments>
and <action>
you must understand how the constructors of Magento 2 objects work. If you override a constructor in Magento, you'll always get a $data-parameter
which is an array. This is the data as provided in the XML files and translated to the internal $_data-array
of MagentoFrameworkDataObject
:
<referenceBlock name="catalog.topnav">
<arguments>
<argument name="template" xsi:type="string">Foo_Bar::buzz.phtml</argument>
</arguments>
</referenceBlock>
...
public function __construct(array $data = [])
// $_data is populated with the arguments from XML:
// so $_data['template'] is now 'Foo_Bar::buzz.phtml'
$this->_data = $data;
However, in the case of a template, if setTemplate()
is used in the pseudo constructor (_construct()
, single underscore), this means that the $data
is overridden, no matter if it's set in the XML.
public function _construct()
$this->setTemplate('foo/bar.phtml');
In that scenario, <action>
is prefered, since this is executed after the constructor & pseudo constructor.
<referenceBlock name="catalog.topnav">
<action method="setTemplate">
<argument name="template" xsi:type="string">Foo_Bar::buzz.phtml</argument>
</action>
</referenceBlock>
add a comment |
To understand the difference between <arguments>
and <action>
you must understand how the constructors of Magento 2 objects work. If you override a constructor in Magento, you'll always get a $data-parameter
which is an array. This is the data as provided in the XML files and translated to the internal $_data-array
of MagentoFrameworkDataObject
:
<referenceBlock name="catalog.topnav">
<arguments>
<argument name="template" xsi:type="string">Foo_Bar::buzz.phtml</argument>
</arguments>
</referenceBlock>
...
public function __construct(array $data = [])
// $_data is populated with the arguments from XML:
// so $_data['template'] is now 'Foo_Bar::buzz.phtml'
$this->_data = $data;
However, in the case of a template, if setTemplate()
is used in the pseudo constructor (_construct()
, single underscore), this means that the $data
is overridden, no matter if it's set in the XML.
public function _construct()
$this->setTemplate('foo/bar.phtml');
In that scenario, <action>
is prefered, since this is executed after the constructor & pseudo constructor.
<referenceBlock name="catalog.topnav">
<action method="setTemplate">
<argument name="template" xsi:type="string">Foo_Bar::buzz.phtml</argument>
</action>
</referenceBlock>
add a comment |
To understand the difference between <arguments>
and <action>
you must understand how the constructors of Magento 2 objects work. If you override a constructor in Magento, you'll always get a $data-parameter
which is an array. This is the data as provided in the XML files and translated to the internal $_data-array
of MagentoFrameworkDataObject
:
<referenceBlock name="catalog.topnav">
<arguments>
<argument name="template" xsi:type="string">Foo_Bar::buzz.phtml</argument>
</arguments>
</referenceBlock>
...
public function __construct(array $data = [])
// $_data is populated with the arguments from XML:
// so $_data['template'] is now 'Foo_Bar::buzz.phtml'
$this->_data = $data;
However, in the case of a template, if setTemplate()
is used in the pseudo constructor (_construct()
, single underscore), this means that the $data
is overridden, no matter if it's set in the XML.
public function _construct()
$this->setTemplate('foo/bar.phtml');
In that scenario, <action>
is prefered, since this is executed after the constructor & pseudo constructor.
<referenceBlock name="catalog.topnav">
<action method="setTemplate">
<argument name="template" xsi:type="string">Foo_Bar::buzz.phtml</argument>
</action>
</referenceBlock>
To understand the difference between <arguments>
and <action>
you must understand how the constructors of Magento 2 objects work. If you override a constructor in Magento, you'll always get a $data-parameter
which is an array. This is the data as provided in the XML files and translated to the internal $_data-array
of MagentoFrameworkDataObject
:
<referenceBlock name="catalog.topnav">
<arguments>
<argument name="template" xsi:type="string">Foo_Bar::buzz.phtml</argument>
</arguments>
</referenceBlock>
...
public function __construct(array $data = [])
// $_data is populated with the arguments from XML:
// so $_data['template'] is now 'Foo_Bar::buzz.phtml'
$this->_data = $data;
However, in the case of a template, if setTemplate()
is used in the pseudo constructor (_construct()
, single underscore), this means that the $data
is overridden, no matter if it's set in the XML.
public function _construct()
$this->setTemplate('foo/bar.phtml');
In that scenario, <action>
is prefered, since this is executed after the constructor & pseudo constructor.
<referenceBlock name="catalog.topnav">
<action method="setTemplate">
<argument name="template" xsi:type="string">Foo_Bar::buzz.phtml</argument>
</action>
</referenceBlock>
edited Dec 13 '18 at 8:17
nikin
778315
778315
answered Jun 28 '16 at 12:32
Giel BerkersGiel Berkers
7,13924282
7,13924282
add a comment |
add a comment |
The following worked for me in Magento EE 2.2.3
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="core.module.block.name" template="[Vendor]_[Module]::path/to/your/template.phtml" />
</body>
</page>
Note: if you are using a custom module to change a core's template and you are going mad because the previous code snipped does not work, make sure your module is loaded after the core module you are trying to change (module.xml) and you executed bin/magento setup:upgrade
:)
This is the cleanest way in my opinion.
– Ben Crook
May 25 '18 at 16:02
add a comment |
The following worked for me in Magento EE 2.2.3
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="core.module.block.name" template="[Vendor]_[Module]::path/to/your/template.phtml" />
</body>
</page>
Note: if you are using a custom module to change a core's template and you are going mad because the previous code snipped does not work, make sure your module is loaded after the core module you are trying to change (module.xml) and you executed bin/magento setup:upgrade
:)
This is the cleanest way in my opinion.
– Ben Crook
May 25 '18 at 16:02
add a comment |
The following worked for me in Magento EE 2.2.3
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="core.module.block.name" template="[Vendor]_[Module]::path/to/your/template.phtml" />
</body>
</page>
Note: if you are using a custom module to change a core's template and you are going mad because the previous code snipped does not work, make sure your module is loaded after the core module you are trying to change (module.xml) and you executed bin/magento setup:upgrade
:)
The following worked for me in Magento EE 2.2.3
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="core.module.block.name" template="[Vendor]_[Module]::path/to/your/template.phtml" />
</body>
</page>
Note: if you are using a custom module to change a core's template and you are going mad because the previous code snipped does not work, make sure your module is loaded after the core module you are trying to change (module.xml) and you executed bin/magento setup:upgrade
:)
edited Apr 6 at 0:14
answered May 25 '18 at 15:52
diazwatsondiazwatson
1,59811425
1,59811425
This is the cleanest way in my opinion.
– Ben Crook
May 25 '18 at 16:02
add a comment |
This is the cleanest way in my opinion.
– Ben Crook
May 25 '18 at 16:02
This is the cleanest way in my opinion.
– Ben Crook
May 25 '18 at 16:02
This is the cleanest way in my opinion.
– Ben Crook
May 25 '18 at 16:02
add a comment |
I don't know why, but i find this way to be the best:
<referenceBlock name="sales.order.items.renderers.default" template="Foo_Bar::sales/order/items/renderer/default.phtml"/>
add a comment |
I don't know why, but i find this way to be the best:
<referenceBlock name="sales.order.items.renderers.default" template="Foo_Bar::sales/order/items/renderer/default.phtml"/>
add a comment |
I don't know why, but i find this way to be the best:
<referenceBlock name="sales.order.items.renderers.default" template="Foo_Bar::sales/order/items/renderer/default.phtml"/>
I don't know why, but i find this way to be the best:
<referenceBlock name="sales.order.items.renderers.default" template="Foo_Bar::sales/order/items/renderer/default.phtml"/>
answered Mar 21 '18 at 13:43
AivorisAivoris
111
111
add a comment |
add a comment |
<referenceBlock name="sales.order.items.renderers.default" template="Foo_Bar::sales/order/items/renderer/default.phtml"/>
This will work only if your block wasn't overwritten before using setTemplate
method. Magento 2.2.x and higher.
add a comment |
<referenceBlock name="sales.order.items.renderers.default" template="Foo_Bar::sales/order/items/renderer/default.phtml"/>
This will work only if your block wasn't overwritten before using setTemplate
method. Magento 2.2.x and higher.
add a comment |
<referenceBlock name="sales.order.items.renderers.default" template="Foo_Bar::sales/order/items/renderer/default.phtml"/>
This will work only if your block wasn't overwritten before using setTemplate
method. Magento 2.2.x and higher.
<referenceBlock name="sales.order.items.renderers.default" template="Foo_Bar::sales/order/items/renderer/default.phtml"/>
This will work only if your block wasn't overwritten before using setTemplate
method. Magento 2.2.x and higher.
answered Sep 25 '18 at 7:00
AleksLiAleksLi
111
111
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f86188%2fmagento-2-changing-a-blocks-template%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown