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.
Asked
Active
Viewed 5.8k times
29

Milan Chandro
- 2,342
- 1
- 18
- 25
4 Answers
72
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();?>

Al Foиce ѫ
- 4,195
- 12
- 39
- 49

Milan Chandro
- 2,342
- 1
- 18
- 25
-
1Calling 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? – DRAJI May 30 '16 at 07:17
-
6this answer is amazing. So much info in one place, that is basically impossible to find in the Magento documentation. – jtate Jul 29 '16 at 13:29
-
Thanks @MilanChandro ! +1 But I have a question. How do I call newsletter phtml using above technique. However the newsletter form is appearing fine but not working??? – Sanchit Gupta Aug 31 '17 at 12:00
6
General convention is
<VendorName_ModuleName>::relative/path/to/phtml/in/templates/
Examples:

Maddy
- 996
- 8
- 18
1
Your custom file path
app/code/{vendor_name}/{module_name}/view/frontend/templates/custom.phtml
calling in phtml file into cms block and pages:-
{{block class="Magento\Framework\View\Element\Template" template="Vendor_Module::custom.phtml"}}
OR
{{block class="Vendor\Module\Block\your_file_name" template="Vendor_Module::custom.phtml"}}
calling in xml layout file:-
<block class="Magento\Framework\View\Element\Template" template="Vendor_Module::custom.phtml">
calling in another phtml file:-
<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Vendor_Module::custom.phtml")->toHtml();?>

Girase Priyanka
- 41
- 3
0
Call phtml template file from within another phtml template file:
<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Theme::test.phtml")->toHtml(); ?>
test.phtml will be located in app/design/frontend/Vendor/themename/Magento_Theme/templates

ivetame
- 213
- 3
- 14