29

我正在创建一个 magento 2 主题。我只想知道如何xml layout, static block, cms page在另一个文件中添加 .phtml.phtml文件。谢谢你。

4

4 回答 4

72

用于改进文档/答案

自定义文件路径

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();?>
于 2015-12-17T06:10:41.200 回答
6

一般约定是 <VendorName_ModuleName>::relative/path/to/phtml/in/templates/

例子:

于 2015-12-15T12:21:45.750 回答
1

您的自定义文件路径

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();?>
于 2020-02-05T12:41:25.167 回答
0

从另一个 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

于 2017-11-25T16:34:09.357 回答