Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 397
Milan Chandro Avatar asked Dec 15 '15 11:12

Milan Chandro


People also ask

How can I call Phtml file in another Phtml file Magento 2?

Re: How to call phtml file in another phtml fileecho $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magestore_Webpos::login. phtml")->toHtml();

How can I call Phtml file in XML file Magento 2?

Call phtml using block code After using the block code phtml file will be called on cms page. If you want to call phtml file on all cms pages then you can create a layout file to achieve this. create layout “cms_page_view. xml” file in your module.


1 Answers

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();?> 
like image 67
Milan Chandro Avatar answered Sep 22 '22 11:09

Milan Chandro