Milind Daraniya

Call Phtml file on CMS Page – Magento2

Published June 22nd, 2023 2 min read

Magento is CMS based framework. You can easily use block in magento page. we will learn how to add or call phtml file on any cms page.

Call phtml using block code

Open any cms page and write block code in content section.

{{block class="Magento\Framework\View\Element\Template" template="Milind_Test::test.phtml"}}

Create your custom template in your module with a mentioned name: 
app/code/Milind/Test/view/frontend/templates/test.phtml

Create layout “cms_page_view.xml” file in your module.
Put the following code in layout file.

<?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>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="test" template="Milind_Test::test.phtml"></block>
        </referenceContainer>
    </body>
</page>

Now you can write this code in layout file phtml file will be called on every cms page.

Thanks.