[英]Magento 2 - How to call a custom phtml file in another phtml file, xml layout, static block and cms page?
我正在创建一个 magento 2 主题。 我只想知道如何在xml layout, static block, cms page
或另一个.phtml
文件中添加.phtml
文件。 谢谢你。
用于改进文档/答案
自定义文件路径
app/design/frontend/{Package}/{theme}/Magento_Theme/templates/html/test.phtml
在xml layout
文件中调用
<block class="Magento\Framework\View\Element\Template" name="test_file" template="Magento_Theme::html/test.phtml"/>
调用blocks and cms pages
{{block class="Magento\Framework\View\Element\Template" name="test_file" template="Magento_Theme::html/test.phtml"}}
调用任何phtml
文件
<?php include ($block->getTemplateFile('Magento_Theme::html/test.phtml')) ?>
或者,和以前一样
<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Theme::html/test.phtml")->toHtml();?>
一般约定是<VendorName_ModuleName>::relative/path/to/phtml/in/templates/
例子:
您的自定义文件路径
app/code/{vendor_name}/{module_name}/view/frontend/templates/custom.phtml
将 phtml 文件调用到 cms 块和页面中:-
{{block class="Magento\Framework\View\Element\Template" template="Vendor_Module::custom.phtml"}}
或者
{{block class="Vendor\Module\Block\your_file_name" template="Vendor_Module::custom.phtml"}}
在 xml 布局文件中调用:-
<block class="Magento\Framework\View\Element\Template" template="Vendor_Module::custom.phtml">
调用另一个 phtml 文件:-
<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Vendor_Module::custom.phtml")->toHtml();?>
从另一个 phtml 模板文件中调用 phtml 模板文件:
<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Theme::test.phtml")->toHtml(); ?>
test.phtml 将位于 app/design/frontend/Vendor/themename/Magento_Theme/templates
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.