Show PHTML File in CMS Page Not Working in Magento2 – How to Fix

magento2phtml

I'm trying to call a phtml file in a CMS Page and I know that there are several question for exactly that question already on Stackexchange. The problem is: its not working. I can't get it to running so obviously I'm doing something wrong right now.

This is my theme structure:
app/design/fronend/MX/base/

The desired phtml file is here:
app/design/fronend/MX/base/MX_Slider/template/carousel.phtml

In my CMS Page I use the following: {{block class="Magento\Framework\View\Element\Template" name="MX" template="MX_base::MX_Slider/template/carousel.phtml"}}

However, the phtml is not showing on the page.

Any ideas why? What am I missing?

Best Answer

Try this

custom file path(Based on your theme you can change it)

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();?>
Related Topic