Tuesday 29 December 2015

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

custom file path

app/code/Magento/Theme/view/frontend/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();?>

5 comments:

  1. Hi My file is in app/design/frontent/vendor_name/theme/Magento_Theme/templates/html/test.phtml

    How to call this files in another phtml file, xml layout, static block and cms page

    ReplyDelete
  2. Calling in blocks and cms pages - method doesn't working... It generates 500 internal server error. (using 2.0.5 version). do you have any idea?

    ReplyDelete
  3. how can i call custom block in magetno 2 cms ?

    ReplyDelete
  4. for "Call Phtml file on CMS Page" Magento 2 check this
    http://www.onlinecode.org/call-phtml-file-cms-page-magento2/

    ReplyDelete