【问题标题】:Magento 2 - How to call a custom phtml file in another phtml file, xml layout, static block and cms page?Magento 2 - 如何在另一个 phtml 文件、xml 布局、静态块和 cms 页面中调用自定义 phtml 文件?
【发布时间】:2016-03-21 04:37:54
【问题描述】:

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

【问题讨论】:

    标签: php magento magento2 magento-2.0


    【解决方案1】:

    用于改进文档/答案

    自定义文件路径

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

    【讨论】:

    • 调用块和 cms 页面 - 方法不起作用...它会生成 500 内部服务器错误。 (使用 2.0.5 版本)。你有什么想法吗?
    • 这个答案太棒了。一个地方有这么多信息,这在 Magento 文档中基本上是不可能找到的。
    • 谢谢@MilanChandro! +1 但我有一个问题。如何使用上述技术调用时事通讯 phtml。但是时事通讯表格看起来很好但不起作用???
    【解决方案2】:
    【解决方案3】:

    您的自定义文件路径

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2016-09-19
        • 1970-01-01
        • 2017-01-17
        • 1970-01-01
        • 2012-09-30
        • 1970-01-01
        • 1970-01-01
        • 2014-04-28
        • 2014-08-19
        相关资源
        最近更新 更多