Skip to content

Commit 90b3c8e

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

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

Diff for: bridge/twig/validation.rst

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
.. code-block:: php
26+
27+
use Symfony\Bridge\Twig\Validator\Constraints\Twig;
28+
29+
class Template
30+
{
31+
#[Twig]
32+
private string $templateCode;
33+
}
34+
35+
If the template contains a syntax error, a validation error will be thrown.
36+
37+
Constraint Options
38+
------------------
39+
40+
**message**
41+
Customize the default error message when the template is invalid.
42+
43+
.. code-block:: php
44+
45+
#[Twig(message: 'Your template contains a syntax error.')]
46+
private string $templateCode;
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+
.. code-block:: php
56+
57+
#[Twig(skipDeprecations: false)]
58+
private string $templateCode;
59+
60+
This can be helpful when enforcing stricter template rules or preparing for major
61+
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)