簡體   English   中英

Magento 2:在 phtml 中調用前端模板

[英]Magento 2: Call frontend template inside phtml

下面的代碼用於從 /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

在上面的代碼中嘗試過這個,但不起作用。

您正在使用的路徑/app/design/frontend/Magento/luma/Custom_Grid/templates/grid1.phtml使它看起來好像您剛剛將代碼放入 Magento 代碼庫中。

永遠不應該那樣做。 要么使用your-theme擴展luma-theme並將代碼放在那里,要么創建自己的模塊並通過 module 擴展活動主題。

使用“主題選項”,您可以直接調用您的模板。

使用“模塊選項”,您可以將grid1.phtml放在YourVendor/YourModule/area/templates/grid1.phtmlareafrontendadminhtml並像這樣調用它:

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

主題繼承請參考Magento 2官方文檔。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM