Latte 3 template engine extensions and utilities, providing commonly used filters for Nette applications.
- JSON Filter: Encode values to JSON with formatting options
- Hash Filters: MD5 and SHA1 hash generation
- Latte 3 Compatible: Built for Latte 3.x using the Extension API
- Auto-Configuration: Automatic registration with Nette DI via Composer
- Linting Support: Works with
latte-lintwithout requiring DI container
- PHP 8.2 or higher
- Latte 3.0.20 or higher
- Nette DI 3.2 or higher
- Nette Utils 4.0 or higher
composer require trejjam/latteRegister the DI extension in config.neon:
extensions:
trejjam.latte: Trejjam\Latte\DI\LatteExtensionThis automatically adds all filters to your Latte engine.
If you prefer to register the Latte extension directly (without the DI extension):
latte:
extensions:
- Trejjam\Latte\TrejjamLatteExtensionFor latte-lint or other tools without Nette DI:
$latte = new Latte\Engine();
$latte->addExtension(new Trejjam\Latte\TrejjamLatteExtension());Encodes value to JSON with granular control options. HTML-safe by default (escapes <, >, &, ', " to prevent XSS).
Basic usage (HTML-safe, unicode-friendly):
{$data|json}Options (multiple string parameters):
pretty- Pretty print with indentationascii- Escape unicode as\uXXXX(for ASCII-only output)html- Enable HTML-safe encoding (default, explicit)!html- Disable HTML-safe encodingforceObjects- Force arrays to objects (empty arrays become{})
Examples:
{* Basic JSON encoding (HTML-safe, unicode) *}
<script type="application/json">{$config|json|noescape}</script>
{* Pretty printed JSON *}
<pre>{$data|json:'pretty'}</pre>
{* Pretty + ASCII-safe (for old browsers) *}
{$data|json:'pretty','ascii'}
{* Disable HTML-safety for API responses *}
{$data|json:'!html'}
{* Force objects + pretty print *}
{$emptyArray|json:'forceObjects','pretty'}
{* Output: {} instead of [] *}
{* Multiple options combined *}
{$data|json:'pretty','ascii','forceObjects'}Default behavior:
- ✅ HTML-safe (escapes
<>&'"as\u003C,\u003E,\u0026,\u0027,\u0022) - ✅ Unicode-friendly (preserves UTF-8 characters like
€,中) - ✅ Compact output (no indentation)
Security note: The default HTML-safe encoding prevents XSS attacks when embedding JSON in HTML <script> tags.
Generates MD5 hash of string.
Usage:
{$email|md5}
{* Output: 5d41402abc4b2a76b9719d911017c592 *}
{* Example: Gravatar URL *}
<img src="https://www.gravatar.com/avatar/{$email|md5}">
{* Example: Cache busting *}
<script id="cache-code">{$timestamp|md5}</script>Generates SHA1 hash of string.
Usage:
{$password|sha1}
{* Output: 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8 *}
{* Example: Simple checksum *}
<meta name="content-hash" content="{$content|sha1}">This package extracts the Latte filters from trejjam/utils and provides them as a standalone Latte 3 extension.
You have two options:
Option A: Keep using trejjam/utils (once it's updated to v4.0)
- The filters will continue to work via the DI extension
Option B: Migrate to trejjam/latte (this package)
- Install:
composer require trejjam/latte - Remove
trejjam.utils.lattefrom extensions inconfig.neon - Add
Trejjam\Latte\TrejjamLatteExtensiontolatte.extensionsinconfig.neon
Update your bin/latte-lint script:
#!/usr/bin/env php
<?php
declare(strict_types=1);
$rootDir = __DIR__ . '/../';
require $rootDir . 'vendor/autoload.php';
$linter = new Latte\Tools\Linter(debug: false, strict: true);
$latte = $linter->getEngine();
$latte->setStrictParsing();
$latte->addExtension(new Trejjam\Latte\TrejjamLatteExtension()); // ← Add this
$ok = $linter->scanDirectory($argv[1] ?? '.');
exit($ok ? 0 : 1);This package includes comprehensive tests using nette/tester with snapshot verification (similar to Verify in C# or Jest snapshots).
make test # Run all tests (37 test cases)
make latte-lint # Lint template fixtures
make all # Run all checks (ECS + PHPStan + latte-lint + tests)- Unit tests (15 test cases): Direct filter testing
- Integration tests (22 test cases):
- Real Latte Engine rendering tests
- Latte linter validation tests
- Snapshot verification for rendered output
The integration tests use snapshot verification to ensure rendered output matches expectations. Snapshots are stored in tests/Integration/__snapshots__/.
Updating snapshots (when filter behavior changes intentionally):
UPDATE_SNAPSHOTS=1 make testSnapshotVerifier API (similar to C# Verify):
$snapshots = new SnapshotVerifier(__FILE__);
// Verify text output
$snapshots->verify($actualOutput, 'test-name');
// Verify JSON output
$snapshots->verifyJson($actualData, 'test-name');
// Verify HTML output
$snapshots->verifyHtml($actualHtml, 'test-name');This package is extracted from trejjam/utils to provide standalone Latte 3 filter support.
The filters were originally implemented in trejjam/utils as:
Trejjam\Utils\Latte\Filter\JsonTrejjam\Utils\Latte\Filter\Md5Trejjam\Utils\Latte\Filter\Sha1
And registered via a Nette DI extension using the deprecated Latte 2.x addFilter() method.
This package implements the same filters using the Latte 3 Extension API (getFilters()), making them:
- Compatible with latte-lint
- Independent of Nette DI
- Following Latte 3 best practices
MIT License. See LICENSE for details.
Jan Trejbal