0

There may be situation when you are developing a Project, you may need to call a custome phtml file in

  • another phtml file
  • cms page
  • cms block (static block)
  • xml layout

How to do it ?

1 Answer 1

5

For the Example purpose we use below custom file, we want to include

app/design/frontend/<vendor>/<theme>/Magento_Theme/templates/extra/test-view.phtml 

Calling in phtml file

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

or alternatively

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

Calling in CMS page and block

Calling in CMS page and block is exactly same

{{block class="Magento\Framework\View\Element\Template" name="test_view" template="Magento_Theme::extra/test-view.phtml"}} 

Calling in XML layout

<block class="Magento\Framework\View\Element\Template" name="test_view" template="Magento_Theme::extra/test-view.phtml"/> 
1
  • +1 Thank you. Very helpful. I tried to add a phtml in my custom theme and called it from Admin CMS block and it works fine. I had to change vars like {{media url=""}} to php variables because the get outputted as they are without getting parsed. PHP variables work fine to get the media path, but is there a standard way to use {{media url=...}} in phtml? Commented May 8, 2019 at 14:44

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.