How do I call a block in a phtml instead of through a layout?

How do I call a block in a phtml instead of through a layout?

August 2, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How do I call a block in a phtml instead of through a layout?

Calling a Block Directly from a PHTML File

In Magento 2, you can call a block directly within a PHTML file instead of adding it to another layout. This is particularly useful when you want to include a specific block in a template without modifying the overall layout structure.

Let’s say we have a catalog_product_view block that we previously added to the product view page using the layout XML:

<block type="catalog/product_view" name="product.info.addtoto" as="addtoto" template="catalog/product/view/addto.phtml"/>

To call this same block directly from an existing PHTML file, you can use Magento’s PHP helper methods.

Here’s how to do it:

  1. Include the necessary PHP files in your PHTML.

    To access Magento’s block and layout functionality within a PHTML file, you need to include two key PHP scripts:

    <?php
    require_once 'app/design/frontend/Magento/blank/template/default/Mage_Page_Block_Html.php';
    require_once 'app/design/frontend/Magento/blank/template/default/Mage_Core_Model_Layout.php';
    ?>
    
  2. Initialize the layout model in your PHTML.

    After including the necessary PHP files, initialize an instance of the Mage_Core_Model_Layout class:

    <php
    $layout = Mage::getSingleton('core/layout');
    ?>
    
  3. Call the specific block within your PHTML file.

    Now that you have the layout object available, you can call the desired block using its name and type:

    <?php echo $layout->createBlock('catalog/product_view', 'addtoto')->setTemplate('catalog/product/view/addto.phtml')->toHtml(); ?>
    

In this example, we’re creating a new instance of the catalog/product_view block with the name “addtoto”. We then set its template to catalog/product/view/addto.phtml and output its HTML content.

Remember that when calling blocks directly in a PHTML file, you should place the PHP code within <php? and ?> tags for proper syntax highlighting and execution by Magento’s rendering engine.

Benefits of Directly Calling Blocks

Calling blocks directly from PHTML has several advantages:

  • Minimal layout modifications: You can include specific functionality without altering the overall layout structure.
  • Improved performance: Direct block inclusion avoids unnecessary layout parsing, which can slightly improve page load times.
  • Better control over the output: By calling a block directly, you have more fine-grained control over its output and placement within your template.

Conclusion

Calling blocks directly from PHTML in Magento 2 is a powerful technique for including specific functionality without modifying the layout. This approach offers improved performance and better control over your templates’ output.

By understanding how to initialize the layout model and call blocks within PHP code embedded in your PHTML files, you can enhance your development workflow and create more dynamic and flexible layouts.

Magento 2 Catalog Themes

Our network