[英]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();?>
一般約定是<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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.