Magento – Calling same template from 2 different layout files Magento2

magento2

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)

Best Answer

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.