bnussbau / trmnl-liquid-laravel
TRMNL specific Liquid filters for Laravel
Fund package maintenance!
Requires
- php: ^8.4
- bacon/bacon-qr-code: ^3.1
- illuminate/contracts: ^12.0||^13.0
- illuminate/support: ^12.0||^13.0
- keepsuit/laravel-liquid: ^0.7.0
- league/commonmark: ^2.10.8
- nesbot/carbon: ^3.11
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^11.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- rector/rector: ^2.6
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
trmnl-liquid-laravel provides a preconfigured Keepsuit Liquid environment with TRMNL-specific tags and filters for rendering plugin templates in Laravel.
It includes:
- a
{% template %}block tag for in-memory partial registration - an enhanced
{% render %}tag that carries plugin context (trmnl,size,data,config) into partials - custom filter sets for data, collections, dates, localization, numbers, markdown, QR code output, and uniqueness helpers
Compatibility
- PHP 8.4+
- Laravel 11, 12, and 13
Installation
You can install the package via composer:
composer require bnussbau/trmnl-liquid-laravel
You can publish the config file with:
php artisan vendor:publish --tag="trmnl-liquid-laravel-config"
Usage
Quick start
use Bnussbau\TrmnlLiquid\TrmnlLiquid; use Keepsuit\Liquid\Render\RenderContext; $environment = (new TrmnlLiquid())->new(); $template = <<<'LIQUID' {% template greeting %}Hello, {{ name }}{% endtemplate %} {% render 'greeting', name: trmnl.user.name %} LIQUID; $output = $environment ->parseString($template) ->render(new RenderContext( data: [ 'trmnl' => ['user' => ['name' => 'Ada']], 'size' => 'full', 'data' => ['source' => 'api'], 'config' => ['refresh' => 300], ], environment: $environment ));
Templates and partials
Use the custom {% template %} block to register an in-memory partial, then render it with {% render %}.
{% template stats/card %}
<div>{{ trmnl.user.name }} has {{ count }} items</div>
{% endtemplate %}
{% render 'stats/card', count: 42 %}
Notes:
- template names must match: letters, numbers, underscores, and slashes
{% template %}outputs nothing; it only registers template content- the enhanced
rendertag injectstrmnl,size,data, andconfigfrom the parent context into the partial
Available filters
The environment registers these custom filters in addition to standard Liquid filters.
Data filters
json- JSON-encode any value without escaping unicode/slashes{{ payload | json }}
parse_json- decode a JSON string into a Liquid object/array{% assign parsed = payload | parse_json %}{{ parsed.foo }}
where_exp- filter collections by expression (and/orsupported){{ towns | where_exp: "town", "town.id < 2 or town.label == 'Boulder'" | map: "label" | join: ", " }}
Collection filters
sample- pick a random element from an array (empty array -> empty output){{ "one,two,three" | split: "," | sample }}
group_by- group collection items by key{% assign grouped = users | group_by: 'age' %}
find_by- return first item matching key/value, with optional fallback{{ users | find_by: 'name', 'Ryan', 'Not Found' }}
map_to_i- cast each iterable item to integer{% assign nums = "1,2,3" | split: "," | map_to_i %}
Number filters
number_with_delimiter(number, delimiter = ",", separator = "."){{ 1234.57 | number_with_delimiter: " ", "," }} -> 1 234,57
number_to_currency(number, unitOrLocale = "$", delimiter = ",", separator = ".", precision = 2){{ 10420 | number_to_currency }} -> $10,420.00{{ 567 | number_to_currency: "sv" }} -> 567.00 kr{{ 123 | number_to_currency: "tbd" }} -> tbd123.00
String and uniqueness filters
pluralize- pluralize with optional explicit plural form{{ "person" | pluralize: 4 }} -> 4 people{{ "person" | pluralize: 4, plural: "humans" }} -> 4 humans
append_random- append 4 random hex chars (useful for DOM IDs){% assign chart_id = "chart-" | append_random %}
url_encode- percent-encode value; arrays are JSON-encoded before encoding{{ payload | url_encode }}
Date and localization filters
days_ago(days, timezone = "Etc/UTC"){{ 10 | days_ago: "Europe/London" }}
ordinalize(dateLike, strftimeFormat)- supports<<ordinal_day>>{{ "2025-10-02" | ordinalize: "%A, %B <<ordinal_day>>, %Y" }}
l_word(word, locale = "en")- localized word lookup (en,es,ko){{ "today" | l_word: "es-ES" }} -> hoy
l_date(value, formatOrLocale, locale = app locale)- localized date formatting{{ "2025-01-11T10:15" | l_date: "shorter" }}{{ "2025-01-11" | l_date: "%y %b", "ko" }} -> 25 1월
Content and rendering filters
markdown_to_html- convert markdown to HTML (raw HTML input is stripped){{ markdown | markdown_to_html }}
qr_code(data, size = 11, level = "", view = "responsive")- render QR SVG{{ "https://example.com" | qr_code }}{{ "https://example.com" | qr_code: 11, "h", "fixed" }}
QR notes:
level:l,m,q,h(defaults toh)view:responsivekeepsviewBox;fixedkeeps explicit width/height
End-to-end template example
{% assign data_json = data | json %}
{% assign parsed = data_json | parse_json %}
{% assign random_id = "chart-" | append_random %}
<h1>{{ trmnl.user.name }}</h1>
<p>{{ "book" | pluralize: parsed.total }}</p>
<p>Updated {{ 1 | days_ago | date: "%b %d, %Y" }}</p>
<div id="{{ random_id }}">
{{ parsed.qr_url | qr_code: 8, "m", "responsive" }}
</div>
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.