Dev.to WebDev 🛠 Dev 👁 2 📖 5 min read

Hyvä and the TALL stack share a frontend: Magento 2 templates vs Laravel Blade

In June I fixed a product gallery on a Magento 2 store that runs Hyvä. The fix was one attribute in a .phtml template: x-on:load.window became x-init. The browser fires load once, after the page's resources, images inclu

In June I fixed a product gallery on a Magento 2 store that runs Hyvä. The fix was one attribute in a .phtml template: x-on:load.window became x-init. The browser fires load once, after the page's resources, images included, have loaded. x-init runs as soon as Alpine starts the component. When I held the images back in a local test, before load fired the gallery showed 1 thumbnail with the old attribute and all 3 with x-init. The same week I replaced a Tailwind safelist pattern on the theme's brand colour prefix, about 9,000 selectors, with an exact list of 42 brand colour classes. In that commit styles.css went from 952 KB to 129 KB.

One change was Alpine and the other Tailwind, two of the four letters in Laravel's TALL stack. You can share that markup between Hyvä templates and Blade components. They differ in who escapes the output and in how the data gets in.

What Hyvä replaced

Luma, Magento's default theme, runs on RequireJS, Knockout and LESS, with jQuery alongside. RequireJS loads on every page, and on a 2.4.9 install without sample data the Luma home page carried 17 Knockout data-bind attributes. By default the LESS files are compiled to CSS on the server, by a PHP port of LESS.

Hyvä keeps layout XML, blocks, view models and .phtml templates. It drops UI components, Knockout, RequireJS and LESS, and uses Tailwind CSS and Alpine.js in their place. Since version 1.4.0, tagged in November 2025, it's open source. Before that every public store needed its own license. Its docs now call it "The open source Magento 2 frontend". It isn't on public Packagist: Composer gets it with a key from a hyva.io account, or from its GitHub repositories. Hyvä Checkout and the other products stay commercial.

The same lines on both sides

TALL is Tailwind, Alpine, Laravel and Livewire. Livewire's JavaScript bundle includes Alpine, so the two load together. The laravel/laravel skeleton ships Vite and Tailwind, with no Alpine or Livewire.

Put a Hyvä template next to a Blade component with the same markup, as in the code below. 6 lines are byte for byte the same: the x-data, the x-on:click, the x-show and the Tailwind classes, with their closing tags. Both rendered the same HTML, apart from whitespace and one escaping detail below.

Blade has a rule of its own for component tags. On a plain HTML element, Alpine's :class passes through. On a Blade component tag, :class="open ? …" is a PHP attribute binding, and rendering stopped with "Undefined constant "open"". Blade's escape for it is ::class. A .phtml doesn't have that rule.

<?php
/** @var \Magento\Framework\Escaper $escaper */
/** @var \Hyva\Theme\Model\ViewModelRegistry $viewModels */
$estimate = $viewModels->require(\Acme\Delivery\ViewModel\Estimate::class);
?>
<div x-data="{ open: false }" class="mt-4">
    <button type="button" class="rounded border px-4 py-2" x-on:click="open = !open">
        <?= $escaper->escapeHtml(__('Delivery estimate')) ?>
    </button>
    <p x-show="open" class="mt-2 text-sm text-gray-600">
        <?= $escaper->escapeHtml($estimate->getText()) ?>
    </p>
</div>
@props(['text'])

<div x-data="{ open: false }" class="mt-4">
    <button type="button" class="rounded border px-4 py-2" x-on:click="open = !open">
        {{ __('Delivery estimate') }}
    </button>
    <p x-show="open" class="mt-2 text-sm text-gray-600">
        {{ $text }}
    </p>
</div>

Who escapes

Since Magento 2.4.0 every .phtml gets $escaper without declaring it: the PHP template engine adds it to each template's variables. Nothing escapes on its own. <?= ?> is plain PHP. With escapeHtml() removed from the template above, <script>alert(1)</script> came out exactly as it went in. Magento's coding standard flags that: phpcs with the Magento2.Security.XssTemplate sniff reported "Unescaped output detected." Nothing at runtime escapes it for you. HTML, attributes, URLs and JavaScript each have their own method: escapeHtml(), escapeHtmlAttr(), escapeUrl(), escapeJs().

Blade compiles {{ $text }} to e($text), which calls htmlspecialchars(). Raw output takes {!! !!}, with one exception: an object that implements Htmlable, an HtmlString for example, prints raw inside {{ }} as well.

escapeHtml() and e() also differ on text that's already escaped. escapeHtml('Tom &amp; Jerry') returns Tom &amp; Jerry, because it calls htmlspecialchars() with double encoding off. e() returns Tom &amp;amp; Jerry, and the browser shows the &amp;. A value stored with HTML entities shows them on a Blade page. Blade::withoutDoubleEncoding() turns that off for {{ }} echoes. On Laravel 11, views compiled before the call keep double encoding until view:clear.

// $escaper is in every .phtml, and nothing escapes by itself
$escaper->escapeHtml($text);
$escaper->escapeHtmlAttr($title);
$escaper->escapeUrl($url);
$escaper->escapeJs($value);

// escapeHtml(): double encoding is off
$escaper->escapeHtml('Tom &amp; Jerry');
// Tom &amp; Jerry
// {{ $text }} compiles to e($text), {!! $html !!} to a plain echo
e('Tom &amp; Jerry');
// Tom &amp;amp; Jerry

// an Htmlable prints raw, inside {{ }} too
e(new HtmlString('<b>bold</b>'));
// <b>bold</b>

// {{ }} without double encoding: Blade::withoutDoubleEncoding()

How the data gets in

Hyvä's theme module adds a plugin to Magento's PHP template engine. It puts a $viewModels registry into every template, in every theme once the module is installed. $viewModels->require(Estimate::class) returns a shared instance from the object manager. It throws if the class doesn't implement Magento's ArgumentInterface. On Hyvä theme pages, the cache tags of a view model that implements IdentityInterface go to the full page cache. The template pulls its data, and layout XML doesn't need an argument for it.

The Blade component above gets its data from the caller: <x-delivery-estimate :text="$text" />. With @props(['text']) and no default, rendering it without the prop stopped with "Undefined variable $text". Blade can pull a service from the container too, with @inject, but a component declares what it needs as props.

The rule

Between Hyvä and the TALL stack, the Alpine attributes and Tailwind classes carry over, with ::class on Blade component tags. They behave the same with Alpine's default build and the same Tailwind major version. Escaping doesn't carry over. In a .phtml it's your job on every output, with the method for its context, and phpcs flags an echo with no escaping at all. In Blade {{ }} does it for you, so you check every {!! !!}, every Htmlable and any stored text with entities. And a Hyvä template pulls its data from the registry, while a Blade component declares its props.

Next: why Magento deploys are heavy, setup:upgrade, di:compile and static content next to a Laravel deploy.

If you write both Hyvä templates and Blade, which habit from one side tripped you up on the other?

Part 14 of 15 of the Magento 2 vs Laravel series. Originally published on LinkedIn.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.