.. index::
single: Symfony2 Twig extensions
Twig is the default template engine for Symfony2. By itself, it already contains
a lot of build-in functions, filters, tags and tests (http://twig.sensiolabs.org/documentation
then scroll to the bottom).
Symfony2 adds more custom extension on top of Twig to integrate some components
into the Twig templates. Below is information about all the custom functions,
filters, tags and tests that are added when using the Symfony2 Core Framework.
There may also be tags in bundles you use that aren't listed here.
.. versionadded:: 2.1
The ``csrf_token``, ``logout_path`` and ``logout_url`` functions were added in Symfony2.1
.. versionadded:: 2.3
The ``render`` and ``controller`` functions are new in Symfony 2.2. Prior,
the ``{% render %}`` tag was used and had a different signature.
Function Syntax
Usage
render(uri, options = {})
render(controller('B:C:a', {params}))
render(path('route', {params}))
render(url('route', {params}))
This will render the fragment for the given controller or URL
For more information, see :ref:`templating-embedding-controller` .
render_esi(controller('B:C:a', {params}))
render_esi(url('route', {params}))
render_esi(path('route', {params}))
This will generates an ESI tag when possible or fallback to the render
behavior otherwise. For more information, see :ref:`templating-embedding-controller` .
render_hinclude(controller(...))
render_hinclude(url('route', {params}))
render_hinclude(path('route', {params}))
This will generates an Hinclude tag for the given controller or URL.
For more information, see :ref:`templating-embedding-controller` .
controller(attributes = {}, query = {})
Used along with the render
tag to refer to the controller that you want to render.
asset(path, packageName = null)
Get the public path of the asset, more information in
":ref:`book-templating-assets` ".
asset_version(packageName = null)
Get the current version of the package, more information in
":ref:`book-templating-assets` ".
form_enctype(view)
This will render the required enctype="multipart/form-data"
attribute
if the form contains at least one file upload field, more information in
in :ref:`the Twig Form reference<reference-forms-twig-enctype>` .
form_widget(view, variables = {})
This will render a complete form or a specific HTML widget of a field,
more information in :ref:`the Twig Form reference<reference-forms-twig-widget>` .
form_errors(view)
This will render any errors for the given field or the "global" errors,
more information in :ref:`the Twig Form reference<reference-forms-twig-errors>` .
form_label(view, label = null, variables = {})
This will render the label for the given field, more information in
:ref:`the Twig Form reference<reference-forms-twig-label>` .
form_row(view, variables = {})
This will render the row (the field's label, errors and widget) of the
given field, more information in :ref:`the Twig Form reference<reference-forms-twig-row>` .
form_rest(view, variables = {})
This will render all fields that have not yet been rendered, more
information in :ref:`the Twig Form reference<reference-forms-twig-rest>` .
csrf_token(intention)
This will render a CSRF token. Use this function if you want CSRF protection without
creating a form
is_granted(role, object = null, field = null)
This will return true
if the current user has the required role, more
information in ":ref:`book-security-template` "
logout_path(key)
This will generate the relative logout URL for the given firewall
logout_url(key)
Equal to logout_path(...)
but this will generate an absolute url
path(name, parameters = {})
Get a relative url for the given route, more information in
":ref:`book-templating-pages` ".
url(name, parameters = {})
Equal to path(...)
but it generates an absolute url
.. versionadded:: 2.1
The ``humanize`` filter was added in Symfony2.1
Filter Syntax
Usage
text|humanize
Makes a technical name human readable (replaces underscores by
spaces and capitalizes the string)
text|trans(arguments = {}, domain = 'messages', locale = null)
This will translate the text into the current language, more
information in :ref:`book-translation-twig` .
text|transchoice(count, arguments = {}, domain = 'messages', locale = null)
This will translate the text with pluralization, more information
in :ref:`book-translation-twig` .
variable|yaml_encode(inline = 0)
This will transform the variable text into a YAML syntax.
variable|yaml_dump
This will render a yaml syntax with their type.
classname|abbr_class
This will render an abbr
element with the short name of a
PHP class.
methodname|abbr_method
This will render a PHP method inside a abbr
element
(e.g. Symfony\Component\HttpFoundation\Response::getContent
arguments|format_args
This will render a string with the arguments of a function and
their types.
arguments|format_args_as_text
Equal to [...]|format_args
, but it strips the tags.
path|file_excerpt(line)
This will render an excerpt of a code file around the given line.
path|format_file(line, text = null)
This will render a file path in a link.
exceptionMessage|format_file_from_text
Equal to format_file
except it parsed the default PHP error
string into a file path (i.e. 'in foo.php on line 45')
path|file_link(line)
This will render a path to the correct file (and line number)
Tag Syntax
Usage
{% form_theme form 'file' %}
This will look inside the given file for overridden form blocks,
more information in :doc:`/cookbook/form/form_customization` .
{% trans with {variables} %}...{% endtrans %}
This will translate and render the text, more information in
:ref:`book-translation-twig`
{% transchoice count with {variables} %}
...
{% endtranschoice %}
This will translate and render the text with pluralization, more
information in :ref:`book-translation-twig`
.. versionadded:: 2.1
The ``selectedchoice`` test was added in Symfony2.1
Test Syntax
Usage
selectedchoice(choice, selectedValue)
This will return true
if the choice is selected for the given form value
Symfony Standard Edition Extensions
The Symfony Standard Edition adds some bundles to the Symfony2 Core Framework.
Those bundles can have other Twig extensions: