Magento 2 call a phtml file in static block

Today we talk about how to in Magento 2 call a phtml file in static block. Calling a phtml file is no different as compared to Magento 1 calling. But if you are calling it in cms block you have to use a class instead of type.

Calling a phtml file on the static block. Just add class and template path.

My current file location for the module is.

app/code/QaisarSatti/HelloWorld/Block/HelloWorld/view/frontend/templates/helloworld.phtml

{{block class="QaisarSatti\HelloWorld\Block\HelloWorld" template="QaisarSatti_HelloWorld::helloworld.phtml"}}

My current phtml location for the theme is.

app/design/frontend/QaisarSatti/helloworld/Magneto_Theme/templates/helloworld.phtml

{{block class="QaisarSatti\HelloWorld\Block\HelloWorld" template="helloworld.phtml"}}

If you want to add phtml with general magento class. Use following code.

{{block class="Magento\Framework\View\Element\Template" template="helloworld.phtml"}}

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!

Leave a Reply