skip to Main Content

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

    $gridHtml = $block->getLayout()->createBlock(
        'MagentoFrameworkViewElementTemplate',
        '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.

2

Answers


  1. Chosen as BEST ANSWER

    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(
    'MagentoFrameworkViewElementTemplate',
    'custom_grid_new'
    )->setTemplate('Magento_CustomGrid::grid.phtml')
    ->toHtml();
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search