Magento 2 - How to call a custom phtml file in another phtml file, xml layout, static block and cms page?

Milan Chandro picture Milan Chandro · Dec 15, 2015 · Viewed 43.3k times · Source

I am creating a magento 2 theme. I just want to know how can I add .phtml file in xml layout, static block, cms page or in another .phtml file. Thank You.

Answer

Milan Chandro picture Milan Chandro · Dec 17, 2015

For improving documentation/answer

Custom file path

app/design/frontend/{Package}/{theme}/Magento_Theme/templates/html/test.phtml

calling in xml layout file

<block class="Magento\Framework\View\Element\Template" name="test_file" template="Magento_Theme::html/test.phtml"/>

Calling in blocks and cms pages

{{block class="Magento\Framework\View\Element\Template" name="test_file" template="Magento_Theme::html/test.phtml"}}

Calling in any phtml file

<?php include ($block->getTemplateFile('Magento_Theme::html/test.phtml')) ?>

OR, as before

<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Theme::html/test.phtml")->toHtml();?>