Magento 2 – How to Properly Add .phtml Files to CMS Home Page

magento2phtmltemplate

I created a few ."phtml" files in my custom theme. Currently, I create a block & widget in the backend to call them on my cms home page.
Is that the correct way?
If I, for example, want to use my custom theme on another magento2 installation, these ".phtml" files aren't included on the cms home page anymore and blocks & widgets need to be created manually.

Should I handle that stuff different? Maybe include into XML? If yes, how?

Thanks a lot for clarification :))

Best Answer

app/design/frontend/Vendor/theme/Magento_Cms/layout/cms_index_index.xml

<?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" template="Magento_Theme::yourphtmlfile.phtml" name="myphtml" />
        </referenceContainer>
    </body>
</page>

or direclty in CMS home page content as follows

{{block class="Magento\Framework\View\Element\Template" template="Magento_Theme::yourphtmlfile.phtml"}}
Related Topic