
Magento 2.x – How to override a template file in a module
If you are creating a new module, where you want to override an existing and used template, you can perform the following and easy step:
- Vendor/MyModule/view/frontend/layout/default.xml (or whichever you need to)
123456789101112<?xml version="1.0"?><page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"><body><referenceContainer name="content"><referenceBlock name="block_i_want_to_override"><action method="setTemplate"><argument name="template" xsi:type="string">Vendor_MyModule::my_new_template.phtml</argument></action></referenceBlock></referenceContainer></body></page> - Vendor/MyModule/view/frontend/templates/my_new_template.phtml
1Hi there, this is my new template
And that’s it, you should now be able to see your new template. Remember to clean cache before that.
Happy coding.!