Skip to content

Commit ac9e20c

Browse files
committed
Add docs for bridge twig validator symfony#20836
1 parent 6c3a18d commit ac9e20c

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

Diff for: bridge/twig/validation.rst

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Validating Twig Template Syntax
2+
===============================
3+
4+
The Twig Bridge provides a custom `Twig` constraint that allows validating
5+
whether a given string contains valid Twig syntax.
6+
7+
This is particularly useful when template content is user-generated or
8+
configurable, and you want to ensure it can be safely rendered by the Twig engine.
9+
10+
Installation
11+
------------
12+
13+
This constraint is part of the `symfony/twig-bridge` package. Make sure it's installed:
14+
15+
.. code-block:: terminal
16+
17+
$ composer require symfony/twig-bridge
18+
19+
Usage
20+
-----
21+
22+
To use the `Twig` constraint, annotate the property that should contain a valid
23+
Twig template::
24+
25+
use Symfony\Bridge\Twig\Validator\Constraints\Twig;
26+
27+
class Template
28+
{
29+
#[Twig]
30+
private string $templateCode;
31+
}
32+
33+
If the template contains a syntax error, a validation error will be thrown.
34+
35+
Constraint Options
36+
------------------
37+
38+
**message**
39+
Customize the default error message when the template is invalid::
40+
41+
// ...
42+
class Template
43+
{
44+
#[Twig(message: 'Your template contains a syntax error.')]
45+
private string $templateCode;
46+
}
47+
48+
**skipDeprecations**
49+
By default, this option is set to `true`, which means Twig deprecation warnings
50+
are ignored during validation.
51+
52+
If you want validation to fail when deprecated features are used in the template,
53+
set this to `false`::
54+
55+
// ...
56+
class Template
57+
{
58+
#[Twig(skipDeprecations: false)]
59+
private string $templateCode;
60+
}
61+
62+
This can be helpful when enforcing stricter template rules or preparing for major
63+
Twig version upgrades.

Diff for: index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Topics
6060
web_link
6161
webhook
6262
workflow
63+
twig_bridge
6364

6465
Components
6566
----------

Diff for: twig_bridge.rst

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Twig Bridge
2+
===========
3+
4+
This bridge integrates Symfony with the Twig templating engine.
5+
6+
.. toctree::
7+
:maxdepth: 1
8+
9+
bridge/twig/validation

0 commit comments

Comments
 (0)