PHPackages                             trejjam/latte - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. trejjam/latte

ActiveLibrary[Templating &amp; Views](/categories/templating)

trejjam/latte
=============

Latte template engine extensions and utilities

v0.2.0(4mo ago)04MITPHPPHP ^8.2 | ^8.3 | ^8.4 | ^8.5CI passing

Since Dec 31Pushed 3mo agoCompare

[ Source](https://github.com/jan-made/latte)[ Packagist](https://packagist.org/packages/trejjam/latte)[ RSS](/packages/trejjam-latte/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (11)Versions (3)Used By (0)

Trejjam Latte Extensions
========================

[](#trejjam-latte-extensions)

[![Latest stable](https://camo.githubusercontent.com/11c4a4692850419674c252d628dd5a09ed435535317a2878f70f74bc23ef6bf0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7472656a6a616d2f6c617474652e737667)](https://packagist.org/packages/trejjam/latte)

Latte 3 template engine extensions and utilities, providing commonly used filters for Nette applications.

Features
--------

[](#features)

- **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-lint` without requiring DI container

Requirements
------------

[](#requirements)

- PHP 8.2 or higher
- Latte 3.0.20 or higher
- Nette DI 3.2 or higher
- Nette Utils 4.0 or higher

Installation
------------

[](#installation)

```
composer require trejjam/latte
```

Configuration
-------------

[](#configuration)

### Nette DI (Recommended)

[](#nette-di-recommended)

Register the DI extension in `config.neon`:

```
extensions:
	trejjam.latte: Trejjam\Latte\DI\LatteExtension
```

This automatically adds all filters to your Latte engine.

### Manual Latte Extension Registration

[](#manual-latte-extension-registration)

If you prefer to register the Latte extension directly (without the DI extension):

```
latte:
	extensions:
		- Trejjam\Latte\TrejjamLatteExtension
```

### Standalone (Without Nette DI)

[](#standalone-without-nette-di)

For **latte-lint** or other tools without Nette DI:

```
$latte = new Latte\Engine();
$latte->addExtension(new Trejjam\Latte\TrejjamLatteExtension());
```

Available Filters
-----------------

[](#available-filters)

### `|json`

[](#json)

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 indentation
- `ascii` - Escape unicode as `\uXXXX` (for ASCII-only output)
- `html` - Enable HTML-safe encoding (default, explicit)
- `!html` - Disable HTML-safe encoding
- `forceObjects` - Force arrays to objects (empty arrays become `{}`)

**Examples**:

```
{* Basic JSON encoding (HTML-safe, unicode) *}
{$config|json|noescape}

{* Pretty printed JSON *}
{$data|json:'pretty'}

{* 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 `` tags.

### `|md5`

[](#md5)

Generates MD5 hash of string.

**Usage**:

```
{$email|md5}
{* Output: 5d41402abc4b2a76b9719d911017c592 *}

{* Example: Gravatar URL *}

{* Example: Cache busting *}
{$timestamp|md5}
```

### `|sha1`

[](#sha1)

Generates SHA1 hash of string.

**Usage**:

```
{$password|sha1}
{* Output: 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8 *}

{* Example: Simple checksum *}

```

Migration from trejjam/utils
----------------------------

[](#migration-from-trejjamutils)

This package extracts the Latte filters from `trejjam/utils` and provides them as a standalone Latte 3 extension.

### If you're using trejjam/utils

[](#if-youre-using-trejjamutils)

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)

1. Install: `composer require trejjam/latte`
2. Remove `trejjam.utils.latte` from extensions in `config.neon`
3. Add `Trejjam\Latte\TrejjamLatteExtension` to `latte.extensions` in `config.neon`

### For latte-lint

[](#for-latte-lint)

Update your `bin/latte-lint` script:

```
#!/usr/bin/env php
