【发布时间】:2016-03-21 04:37:54
【问题描述】:
我正在创建一个 magento 2 主题。我只想知道如何在 xml layout, static block, cms page 或另一个 .phtml 文件中添加 .phtml 文件。谢谢。
【问题讨论】:
标签: php magento magento2 magento-2.0
我正在创建一个 magento 2 主题。我只想知道如何在 xml layout, static block, cms page 或另一个 .phtml 文件中添加 .phtml 文件。谢谢。
【问题讨论】:
标签: php magento magento2 magento-2.0
用于改进文档/答案
自定义文件路径
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
【讨论】: