Magento 2: Changing a Block's Templatehow 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

Is it legal for company to use my work email to pretend I still work there?

What is a clear way to write a bar that has an extra beat?

How can bays and straits be determined in a procedurally generated map?

How to format long polynomial?

Can a Cauchy sequence converge for one metric while not converging for another?

Fully-Firstable Anagram Sets

Languages that we cannot (dis)prove to be Context-Free

A case of the sniffles

Are astronomers waiting to see something in an image from a gravitational lens that they've already seen in an adjacent image?

Why do I get two different answers for this counting problem?

Was any UN Security Council vote triple-vetoed?

Can you really stack all of this on an Opportunity Attack?

Malcev's paper "On a class of homogeneous spaces" in English

Does detail obscure or enhance action?

Are the number of citations and number of published articles the most important criteria for a tenure promotion?

Has there ever been an airliner design involving reducing generator load by installing solar panels?

tikz convert color string to hex value

Today is the Center

Important Resources for Dark Age Civilizations?

"You are your self first supporter", a more proper way to say it

What is the word for reserving something for yourself before others do?

I'm flying to France today and my passport expires in less than 2 months

Definite integral giving negative value as a result?

When a company launches a new product do they "come out" with a new product or do they "come up" with a new product?



Magento 2: Changing a Block's Template


how 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;








45















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.










share|improve this question






























    45















    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.










    share|improve this question


























      45












      45








      45


      10






      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.










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 12 '15 at 20:32







      Alan Storm

















      asked Oct 12 '15 at 15:45









      Alan StormAlan Storm

      29.2k21119309




      29.2k21119309




















          6 Answers
          6






          active

          oldest

          votes


















          52














          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>





          share|improve this answer

























          • 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



















          41














          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>





          share|improve this answer

























          • 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





            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


















          26














          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>





          share|improve this answer
































            8














            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 :)






            share|improve this answer

























            • This is the cleanest way in my opinion.

              – Ben Crook
              May 25 '18 at 16:02


















            1














            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"/>





            share|improve this answer






























              1














              <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.






              share|improve this answer























                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
                );



                );













                draft saved

                draft discarded


















                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









                52














                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>





                share|improve this answer

























                • 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
















                52














                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>





                share|improve this answer

























                • 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














                52












                52








                52







                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>





                share|improve this answer















                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>






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Oct 10 '17 at 15:34









                Roman Snitko

                374212




                374212










                answered Oct 12 '15 at 15:52









                Mage2.PROMage2.PRO

                3,68111319




                3,68111319












                • 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






                • 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














                41














                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>





                share|improve this answer

























                • 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





                  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















                41














                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>





                share|improve this answer

























                • 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





                  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













                41












                41








                41







                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>





                share|improve this answer















                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>






                share|improve this answer














                share|improve this answer



                share|improve this answer








                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 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





                  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






                • 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











                26














                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>





                share|improve this answer





























                  26














                  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>





                  share|improve this answer



























                    26












                    26








                    26







                    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>





                    share|improve this answer















                    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>






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 13 '18 at 8:17









                    nikin

                    778315




                    778315










                    answered Jun 28 '16 at 12:32









                    Giel BerkersGiel Berkers

                    7,11924281




                    7,11924281





















                        8














                        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 :)






                        share|improve this answer

























                        • This is the cleanest way in my opinion.

                          – Ben Crook
                          May 25 '18 at 16:02















                        8














                        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 :)






                        share|improve this answer

























                        • This is the cleanest way in my opinion.

                          – Ben Crook
                          May 25 '18 at 16:02













                        8












                        8








                        8







                        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 :)






                        share|improve this answer















                        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 :)







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited 4 hours ago

























                        answered May 25 '18 at 15:52









                        diazwatsondiazwatson

                        1,58811425




                        1,58811425












                        • 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





                        This is the cleanest way in my opinion.

                        – Ben Crook
                        May 25 '18 at 16:02











                        1














                        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"/>





                        share|improve this answer



























                          1














                          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"/>





                          share|improve this answer

























                            1












                            1








                            1







                            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"/>





                            share|improve this answer













                            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"/>






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 21 '18 at 13:43









                            AivorisAivoris

                            111




                            111





















                                1














                                <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.






                                share|improve this answer



























                                  1














                                  <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.






                                  share|improve this answer

























                                    1












                                    1








                                    1







                                    <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.






                                    share|improve this answer













                                    <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.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Sep 25 '18 at 7:00









                                    AleksLiAleksLi

                                    111




                                    111



























                                        draft saved

                                        draft discarded
















































                                        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.




                                        draft saved


                                        draft discarded














                                        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





















































                                        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







                                        Popular posts from this blog

                                        Can not update quote_id field of “quote_item” table magento 2Magento 2.1 - We can't remove the item. (Shopping Cart doesnt allow us to remove items before becomes empty)Add value for custom quote item attribute using REST apiREST API endpoint v1/carts/cartId/items always returns error messageCorrect way to save entries to databaseHow to remove all associated quote objects of a customer completelyMagento 2 - Save value from custom input field to quote_itemGet quote_item data using quote id and product id filter in Magento 2How to set additional data to quote_item table from controller in Magento 2?What is the purpose of additional_data column in quote_item table in magento2Set Custom Price to Quote item magento2 from controller

                                        How to solve knockout JS error in Magento 2 Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?(Magento2) knockout.js:3012 Uncaught ReferenceError: Unable to process bindingUnable to process binding Knockout.js magento 2Cannot read property `scopeLabel` of undefined on Product Detail PageCan't get Customer Data on frontend in Magento 2Magento2 Order Summary - unable to process bindingKO templates are not loading in Magento 2.1 applicationgetting knockout js error magento 2Product grid not load -— Unable to process binding Knockout.js magento 2Product form not loaded in magento2Uncaught ReferenceError: Unable to process binding “if: function()return (isShowLegend()) ” magento 2

                                        Nissan Patrol Зміст Перше покоління — 4W60 (1951-1960) | Друге покоління — 60 series (1960-1980) | Третє покоління (1980–2002) | Четверте покоління — Y60 (1987–1998) | П'яте покоління — Y61 (1997–2013) | Шосте покоління — Y62 (2010- ) | Посилання | Зноски | Навігаційне менюОфіційний український сайтТест-драйв Nissan Patrol 2010 7-го поколінняNissan PatrolКак мы тестировали Nissan Patrol 2016рвиправивши або дописавши її