簡體   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