Piyush Dankhra

About me
moon indicating dark mode
sun indicating light mode

Last updated: May 28, 2025

How template fallback works in the Hyvä theme

In this blog, we’ll explore how template fallback works in the Hyvä theme and understand how templates can be overridden in two ways:

  1. In the Hyvä compatibility module.
  2. In the Hyvä child theme.

Table of contents

  1. The template in the Hyvä compatibility module
  2. Override the template in the Hyvä child theme under the Original module path
  3. Override the template in the Hyvä child theme under the Hyvä-compatible module path
  4. Best Practice

Step 1: The template in the Hyvä compatibility module

Suppose a module named MyCompany_SampleModule contains the following template file: MyCompany_SampleModule::view/frontend/templates/sampleblock.phtml:

<?php
/**
* @var $block \Magento\Framework\View\Element\Template
* @var $escaper \Magento\Framework\Escaper
*/
?>
<div class="row">
<?= $escaper->escapeHtml(__('Hello, this is a sample block in a MyCompany_SampleModule::sampleblock.phtml module!')) ?>
</div>

This template file is overridden in the Hyvä compatibility module: Hyva_MyCompanySampleModule::view/frontend/templates/sampleblock.phtml

<?php
/**
* @var $block \Magento\Framework\View\Element\Template
* @var $escaper \Magento\Framework\Escaper
*/
?>
<div class="py-2">
<?= $escaper->escapeHtml(__('Hello, this is a sample block in a Hyva_MyCompanySampleModule::sampleblock.phtml module!')) ?>
</div>

On the frontend, you’ll see the following text:

Hello, this is a sample block in a Hyva_MyCompanySampleModule::sampleblock.phtml module!

Step 2: Override the template in the Hyvä child theme under the Original module path

To override the template file from the Hyva_MyCompanySampleModule::sampleblock.phtml in your Hyvä child theme, place your template under the original module path MyCompany_SampleModule rather than the Hyvä-compatible module path.

Template file path: app/design/frontend/MyCompany/child/MyCompany_SampleModule/templates/sampleblock.phtml

<?php
/**
* @var $block \Magento\Framework\View\Element\Template
* @var $escaper \Magento\Framework\Escaper
*/
?>
<div>
<?= $escaper->escapeHtml(__('Hello, this is a sample block in a Hyva child theme MyCompany_SampleModule module!')) ?>
</div>

On the frontend, you’ll see: Hello, this is a sample block in a Hyva child theme MyCompany_SampleModule module!

Step 3: Override the template in the Hyvä child theme under the Hyvä-compatible module path

To override the template file in the Hyvä child theme under the Hyvä-compatible module path, place the template file at app/design/frontend/MyCompany/child/Hyva_MyCompanySampleModule/templates/sampleblock.phtml.

<?php
/**
* @var $block \Magento\Framework\View\Element\Template
* @var $escaper \Magento\Framework\Escaper
*/
?>
<div>
<?= $escaper->escapeHtml(__('Hello, this is a sample block in a Hyva child theme Hyva_MyCompanySampleModule module!')) ?>
</div>

Surprisingly, the frontend will display the text from the MyCompany_SampleModule::sampleblock.phtml template instead of Hyva_MyCompanySampleModule::sampleblock.phtml: Hello, this is a sample block in a Hyva child theme MyCompany_SampleModule module!

Why?

The Hyvä compat module fallback mechanism follows this sequence:

  1. Checks in the Hyvä child theme under the original module path (MyCompany_SampleModule::sampleblock.phtml).
  2. If not found, checks in the Hyvä child theme under the Hyvä-compatible module path (Hyva_MyCompanySampleModule::sampleblock.phtml).
  3. If still not found, checks in the Hyvä compatibility module (Hyva_MyCompanySampleModule::sampleblock.phtml).
  4. Finally, if none are found, checks in the original module (MyCompany_SampleModule::sampleblock.phtml).

For example, if you delete the template file MyCompany_SampleModule::sampleblock.phtml from the Hyvä child theme, the text of the template file Hyva_MyCompanySampleModule::sampleblock.phtml will be displayed: Hello, this is a sample block in a Hyva child theme Hyva_MyCompanySampleModule module!

Step 4: Best Practice

In standard Magento development, it’s common practice to override template files in the child theme under the same module path as the original template.

However, in the Hyvä theme, it’s not recommended to override templates under the Hyvä-compatible module path (Hyva_MyCompanySampleModule) within the Hyvä child theme.

Always override templates under the original module path (MyCompany_SampleModule) instead of the Hyvä-compatible module path (Hyva_MyCompanySampleModule).

If a custom template file is added exclusively in the Hyvä-compatible module (one that doesn’t exist in the original module), you should place the template in the Hyvä-compatible module path within your Hyvä child theme.


About me.
Adobe Certified Expert - Adobe Commerce Developer / Front-End Developer / Business Practitioner
Adobe Subject Matter Expert - Adobe Commerce Front End Developer / Business Practitioner
Working on Magento 2 / Hyvä