Mr.Devops
Mr.Devops

Reputation: 306

Magento 2: Call frontend template inside phtml

This code below is used to call the template from /app/code/ module.

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

How to set the template from following path

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

tried this above in above code, but is not working.

Upvotes: 0

Views: 1326

Answers (2)

Mr.Devops
Mr.Devops

Reputation: 306

Reference: 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();

Upvotes: 0

Tobi
Tobi

Reputation: 633

That path you are using /app/design/frontend/Magento/luma/Custom_Grid/templates/grid1.phtml makes it look as if you have just put your code insde the Magento Codebase.

You should never do that. Either extend luma-theme with your-theme and put your code there, or create your own module and extend the active theme via module.

With the "Theme option" you can call your templates directly.

With the "Module option" you can put your grid1.phtml in YourVendor/YourModule/area/templates/grid1.phtml while area is either frontend or adminhtml and call it like this:

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

Please refer to Magento 2 official documentation for theme inheritance.

Upvotes: 1

Related Questions