Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7a124d8

Browse files
committed
[#1039] Adding a reference section for the global twig variables
1 parent 6af2f78 commit 7a124d8

File tree

1 file changed

+63
-4
lines changed

1 file changed

+63
-4
lines changed

reference/forms/twig_reference.rst

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
.. index::
22
single: Forms; Twig form function reference
33

4-
Twig Template Form Function Reference
5-
=====================================
4+
Twig Template Form Function and Variable Reference
5+
==================================================
6+
7+
When working with forms in a template, there are two powerful things at your
8+
disposal:
9+
10+
* :ref:`Functions<reference-form-twig-functions>` for rendering each part of a form
11+
* :ref:`Variables<twig-reference-form-variables>` for getting *any* information about any field
12+
13+
You'll use functions often to render your fields. Variables, on the other
14+
hand, are less commonly-used, but infinitely powerful since you can access
15+
a fields label, id attribute, errors, and anything else about the field.
16+
17+
.. _reference-form-twig-functions:
18+
19+
Form Rendering Functions
20+
------------------------
621

722
This reference manual covers all the possible Twig functions available for
823
rendering forms. There are several different functions available, and each
@@ -114,8 +129,10 @@ good idea to include this in your form tag:
114129

115130
.. _`twig-reference-form-variables`:
116131

117-
More about Form "Variables"
118-
---------------------------
132+
More about Form Variables
133+
-------------------------
134+
135+
For a full list of variables, see: :ref:`reference-form-twig-variables`.
119136

120137
In almost every Twig function above, the final argument is an array of "variables"
121138
that are used when rendering that one part of the form. For example, the
@@ -174,4 +191,46 @@ to see what options you have available.
174191
{# does **not** work - the variables are not recursive #}
175192
{{ form_widget(form, { 'attr': {'class': 'foo'} }) }}
176193
194+
.. _reference-form-twig-variables:
195+
196+
Form Variables Reference
197+
~~~~~~~~~~~~~~~~~~~~~~~~
198+
199+
The following variables are common to every field type. Certain field types
200+
may have even more variables and some variables here only really apply to
201+
certain types.
202+
203+
+-----------------+-----------------------------------------------------------------------------------------+
204+
| Variable | Usage |
205+
+=================+=========================================================================================+
206+
| ``id`` | The ``id`` HTML attribute to be rendered |
207+
+-----------------+-----------------------------------------------------------------------------------------+
208+
| ``name`` | The name of the field (e.g. ``title``) - but not the ``name`` |
209+
| | HTML attribute, which is ``full_name`` |
210+
+-----------------+-----------------------------------------------------------------------------------------+
211+
| ``full_name`` | The ``name`` HTML attribute to be rendered |
212+
+-----------------+-----------------------------------------------------------------------------------------+
213+
| ``errors`` | An array of any errors attached to *this* specific field (e.g. ``form.title.errors``). |
214+
| | Note that you can't use ``form.errors`` to determine if a form is valid, |
215+
| | since this only returns "global" errors: some individual fields may have errors |
216+
+-----------------+-----------------------------------------------------------------------------------------+
217+
| ``value`` | The value that will be used when rendering (commonly the ``value`` HTML attribute) |
218+
+-----------------+-----------------------------------------------------------------------------------------+
219+
| ``read_only`` | If ``true``, ``disabled="disabled"`` is added to the field |
220+
+-----------------+-----------------------------------------------------------------------------------------+
221+
| ``required`` | If ``true``, a ``required`` attribute is added to the field to activate HTML5 |
222+
| | validation. Additionally, a ``required`` class is added to the label. |
223+
+-----------------+-----------------------------------------------------------------------------------------+
224+
| ``max_length`` | Adds a ``maxlength`` HTML attribute to the element |
225+
+-----------------+-----------------------------------------------------------------------------------------+
226+
| ``pattern`` | Adds a ``pattern`` HTML attribute to the element |
227+
+-----------------+-----------------------------------------------------------------------------------------+
228+
| ``label`` | The string label that will be rendered |
229+
+-----------------+-----------------------------------------------------------------------------------------+
230+
| ``multipart`` | If ``true``, ``form_enctype`` will render ``enctype="multipart/form-data"``. |
231+
| | This only applies to the parent form element. |
232+
+-----------------+-----------------------------------------------------------------------------------------+
233+
| ``attr`` | A key-value array that will be rendered as HTML attributes on the field |
234+
+-----------------+-----------------------------------------------------------------------------------------+
235+
177236
.. _`form_div_layout.html.twig`: https://github.com/symfony/symfony/blob/2.0/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

0 commit comments

Comments
 (0)