voku / anti-xss-twig
Twig extension for XSS protection.
3.0.1
2018-10-27 23:50 UTC
Requires
- php: >=7.0.0
- twig/twig: ^1.26|~2.0
- voku/anti-xss: ~4.1
Requires (Dev)
- phpunit/phpunit: ~6.0
This package is auto-updated.
Last update: 2026-08-13 13:33:03 UTC
README
AntiXSS for Twig
Description
A Twig extension for voku/anti-xss.
Currently supported Twig features are:
-
Tag (recommended)
{% xss_clean %} <foo>bar</foo> {% end_xss_clean %}
-
Function
{{ xss_clean(' <foo>bar</foo>') }}
-
Filter
{{ '<foo>bar</foo>' | xss_clean }}
Installation
- Install and use composer in your project.
- Require this package via composer:
composer require voku/anti-xss-twig
Usage
This package targets modern PHP and Twig 3.x setups.
First register the extension with Twig:
use Twig\Environment; use Twig\Loader\FilesystemLoader; use voku\helper\AntiXSS; use voku\twig\AntiXssExtension; $loader = new FilesystemLoader('/path/to/templates'); $twig = new Environment($loader); $antiXss = new AntiXSS(); $twig->addExtension(new AntiXssExtension($antiXss));
Then use it in your templates:
{% xss_clean %} <foo>bar</foo> {% end_xss_clean %}
$twig->addExtension(new AntiXssExtension($antiXss));
Run the test suite with:
composer test
History
See CHANGELOG for the full history of changes.