【问题标题】:Magento 2 : How to call template file from block fileMagento 2:如何从块文件中调用模板文件
【发布时间】:2022-01-06 22:46:27
【问题描述】:

所以我想直接从块文件中调用模板 phtml 文件,而不是将它们附加到布局 XML 文件中。

提前谢谢你!

【问题讨论】:

    标签: php magento2 block


    【解决方案1】:

    如果你想在没有 xml 的情况下调用它,为什么不直接从 phtml via

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

    你可能在你的块中使用相同的方式,但我没有测试过。

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $content = $objectManager->create('Magento\Framework\View\Element\Template');
    $content->setTemplate('Vendor_Module::template.phtml');
    $content->toHtml();
    

    【讨论】:

    • 不,我尝试过这种方法,但对我不起作用,如果您有其他可行的解决方案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    相关资源
    最近更新 更多