【问题标题】:Magento 2: Call frontend template inside phtmlMagento 2:在 phtml 中调用前端模板
【发布时间】:2020-11-23 09:15:53
【问题描述】:

下面这段代码用于从/app/code/模块调用模板。

    $gridHtml = $block->getLayout()->createBlock(
        '\Magento\Framework\View\Element\Template',
        'custom_grid_new'
    )->setTemplate('Magento_CustomGrid::product/view/templates/grid.phtml')
        ->toHtml();

如何从以下路径设置模板

/app/design/frontend/Magento/luma/Custom_Grid/templates/grid1.phtml

在上面的代码中试过这个,但没有用。

【问题讨论】:

    标签: magento magento2 magento-2.3


    【解决方案1】:

    您使用的路径 /app/design/frontend/Magento/luma/Custom_Grid/templates/grid1.phtml 看起来好像您刚刚将代码放入 Magento 代码库。

    你应该永远不要那样做。使用your-theme 扩展luma-theme 并将您的代码放在那里,或者创建您自己的模块并通过模块扩展活动主题。

    使用“主题选项”,您可以直接调用您的模板。

    使用“模块选项”,您可以将grid1.phtml 放入YourVendor/YourModule/area/templates/grid1.phtml,而areafrontendadminhtml,并像这样调用它:

    ->setTemplate('YourVendor_YourModule::grid1.phtml')->toHtml();
    

    主题继承请参考 Magento 2 官方文档。

    【讨论】:

    • 谢谢,但我找到了 magento 社区中某人回答的解决方案!添加为答案
    【解决方案2】:

    参考:https://community.magento.com/t5/Magento-2-x-Programming/Magento-2-Call-phtml-path-from-frontend-folder/m-p/463320/thread-id/12091#M12094

    $gridHtml = $block->getLayout()->createBlock(
    '\Magento\Framework\View\Element\Template',
    'custom_grid_new'
    )->setTemplate('Magento_CustomGrid::grid.phtml')
    ->toHtml();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多