繁体   English   中英

Magento 2 - 如何在另一个 phtml 文件、xml 布局、静态块和 cms 页面中调用自定义 phtml 文件?

[英]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();?>

您的自定义文件路径

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM