magehx / mage-template-utils
Magento 2 module that provides powerful global utility features to the templates.
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
Open Issues: 0
Type:magento2-module
Requires
- magento/framework: *
README
A Magento 2 module that introduces convenient global utilities for use in .phtml
templates, helping you write cleaner, more expressive code.
β¨ Features
π Escaper Shortcuts
Use simple closure aliases instead of long $escaper
method calls:
Alias | Equivalent |
---|---|
$eHtml() |
$escaper->escapeHtml() |
$eJs() |
$escaper->escapeJs() |
$eUrl() |
$escaper->escapeUrl() |
$eHtmlAttr() |
$escaper->escapeHtmlAttr() |
Example:
<?= $eHtml(__('Hello world!')) ?>
Instead of:
<?= $escaper->escapeHtml(__('Hello world!')) ?>
π¦ ViewModel Provider β $viewModelProvider
Fetch a ViewModel instance directly inside a .phtml
file without having to declare it in layout XML:
<?php use Namespace\Module\ViewModel\YourViewModel; /** @var Closure $viewModelProvider */ $viewModel = $viewModelProvider(YourViewModel::class); ?>
π§Ύ Form Key Helper β $formKey
Insert a hidden form key input in one line:
<form> <?= $formKey ?> ... </form>
Simple, secure, and saves time.
π CSP Nonce Provider β $nonce
Generate a valid CSP nonce for inline scripts:
<script nonce="<?= $eAttr($nonce) ?>"> // safe inline script </script>
Essential for security-conscious development.
π¨ Conditional Class Names β $classNames
Apply dynamic class names with ease and clarity:
Before:
<div class="flex items-center <?= $escaper->escapeHtmlAttr($condition1 ? 'justify-between' : 'justify-center') ?> <?= $escaper->escapeHtmlAttr($i > 10 ? 'bg-orange' : '') ?>"> ... </div>
After:
<div class="<?= $classNames([ 'flex items-center', $condition1 ? 'justify-between' : 'justify-center', 'bg-orange' => $i > 10 ]) ?>"> ... </div>
Improves readability and maintainability of conditional UI.
π¦ Installation
composer require magehx/mage-template-utils
π Requirements
- PHP 8.1+
π οΈ License
MIT β free to use and modify.
π¬ Feedback / Contributions
PRs and issues are welcome. Letβs make Magento templating less painful!