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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34288366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:53:05  来源:igfitidea点击:

Magento 2 - How to call a custom phtml file in another phtml file, xml layout, static block and cms page?

phpmagentomagento2magento-2.0

提问by Milan Chandro

I am creating a magento 2 theme. I just want to know how can I add .phtml file in xml layout, static block, cms pageor in another .phtmlfile. Thank You.

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

回答by Milan Chandro

For improving documentation/answer

用于改进文档/答案

Custom file path

自定义文件路径

app/design/frontend/{Package}/{theme}/Magento_Theme/templates/html/test.phtml

calling in xml layoutfile

调用xml layout文件

<block class="Magento\Framework\View\Element\Template" name="test_file" template="Magento_Theme::html/test.phtml"/>

Calling in blocks and cms pages

呼入 blocks and cms pages

{{block class="Magento\Framework\View\Element\Template" name="test_file" template="Magento_Theme::html/test.phtml"}}

Calling in any phtmlfile

调用任意phtml文件

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

回答by ivetame

Call phtml template file from within another phtml template file:

从另一个 phtml 模板文件中调用 phtml 模板文件:

<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Theme::test.phtml")->toHtml(); ?>

test.phtml will be located in app/design/frontend/Vendor/themename/Magento_Theme/templates

test.phtml 将位于 app/design/frontend/Vendor/themename/Magento_Theme/templates

回答by Girase Priyanka

Your custom file path

您的自定义文件路径

app/code/{vendor_name}/{module_name}/view/frontend/templates/custom.phtml

calling in phtml file into cms block and pages:-

将 phtml 文件调用到 cms 块和页面中:-

{{block class="Magento\Framework\View\Element\Template" template="Vendor_Module::custom.phtml"}}

OR

或者

{{block class="Vendor\Module\Block\your_file_name" template="Vendor_Module::custom.phtml"}}

calling in xml layout file:-

在 xml 布局文件中调用:-

<block class="Magento\Framework\View\Element\Template" template="Vendor_Module::custom.phtml">

calling in another phtml file:-

调用另一个 phtml 文件:-

<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Vendor_Module::custom.phtml")->toHtml();?>