2

Is it possible to keep the same templates phtml files in different modules and call them in different layout files?

Eg: i have a template1.phtml in Vendor/Module1/view/front/templates/template1.phtml

where template1.phtml is called from Module1's layout file.

I need to keep the same content of template1 in template2.phtml

and can I call that template with Modules two's layout file?

If I did so, template2 is not getting called!!

UPDATE:

\Vendor\Module1\view\frontend\layout\catalogsearch_result_index.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock class="Vendor\Module1\Block\Navigation" before="-" name="catalogsearch.leftnav">
      <action method="setTemplate">
        <argument name="template" xsi:type="string">Vendor_Module1::layer/view.phtml</argument>
      </action>
    </referenceBlock>
</body>

view.phtml in templates/layer folder

** IN Vendor\Module2\view\frontend\layout\m_result_index.xml**

 <referenceBlock class="Vendor\Module1\Block\Navigation" before="-" name="catalogsearch.leftnav">
      <action method="setTemplate">
        <argument name="template" xsi:type="string">Vendor_Module1::layer/view.phtml</argument>
      </action>
    </referenceBlock>

Here, calling the same view.phtml in this layout file.

But it's not getting called. (I need to get the filters in the product page, which is coming from 1st modules layout file, but not from a 2nd layout file)

5
  • at which place you have declared xml code for template2.phtml // or please show your code for declaration of template1.phtml and template2.phtml filie Sep 19, 2016 at 6:59
  • Please post your layout.xml file
    – Jackson
    Sep 19, 2016 at 7:13
  • Updated both the modules layout file
    – Sushivam
    Sep 19, 2016 at 7:14
  • Any changes to be done or update on the layout file @Ankit?
    – Sushivam
    Sep 19, 2016 at 12:21
  • Not sure @SachinS Let me check & revert ASAP
    – Jackson
    Sep 20, 2016 at 1:16

2 Answers 2

0

You have to keep below code for your second xml file,

Instead of call Vendor\Module1 you have to keep Vendor_Module1

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock class="Vendor\Module1\Block\Navigation" before="-" name="catalogsearch.leftnav">
      <action method="setTemplate">
        <argument name="template" xsi:type="string">Vendor_Module1::layer/view.phtml</argument>
      </action>
    </referenceBlock>
</body>

Remove var and clear cache try again.

2
  • Sorry @Rakesh, i have updated the code, i have replaced the _ with / while changing the Vendor/Module...If Vendor_Module1 is the only change, then thats what i have used and cleared all cache too!
    – Sushivam
    Sep 19, 2016 at 7:37
  • any update @Rakesh ?
    – Sushivam
    Sep 19, 2016 at 9:13
0

Take the reference of vendor\magento\module-checkout\view\frontend\layout\checkout_cart_configure.xml

/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="catalog_product_view"/>
    <body>
        <referenceBlock name="product.info.addtocart">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Magento_Checkout::cart/item/configure/updatecart.phtml</argument>
            </action>
        </referenceBlock>
        <referenceBlock name="product.info.addtocart.additional">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Magento_Checkout::cart/item/configure/updatecart.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

or you can also do

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="content">
            <block class="Custom\Module\Block\Posts\ViewPosts" name="custom_module.posts.view" template="posts/view.phtml" />
            <block class="Custom\Module\Block\ListIndex" name="custom_module_mylist_block" template="mylist.phtml" cacheable="false" />
        </referenceBlock>
    </body>
</page>

Make sure you have correct template path, otherwise it will be not called.

I have checked & it's working fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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