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 ?

3

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 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? – Mohammed Joraid May 8 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.