Symfony2 Twig Extensions
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.
Functions¶
New in version 2.1: The csrf_token
, logout_path
and logout_url
functions were added in Symfony 2.1
New in version 2.2: 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 Embedding Controllers. |
render_esi(controller('B:C:a', {params}))
render_esi(url('route', {params}))
render_esi(path('route', {params})) |
This will generate an ESI tag when possible or fallback to the render
behavior otherwise. For more information, see Embedding Controllers. |
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 Embedding Controllers. |
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 "Linking to Assets". |
asset_version(packageName = null) |
Get the current version of the package, more information in "Linking to 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 the Twig Form reference. |
form_widget(view, variables = {}) |
This will render a complete form or a specific HTML widget of a field, more information in the Twig Form reference. |
form_errors(view) |
This will render any errors for the given field or the "global" errors, more information in the Twig Form reference. |
form_label(view, label = null, variables = {}) |
This will render the label for the given field, more information in the Twig Form reference. |
form_row(view, variables = {}) |
This will render the row (the field's label, errors and widget) of the given field, more information in the Twig Form reference. |
form_rest(view, variables = {}) |
This will render all fields that have not yet been rendered, more information in the Twig Form reference. |
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 "Access Control in Templates" |
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 "Linking to Pages". |
url(name, parameters = {}) |
Equal to path(...) but it generates an absolute URL |
Filters¶
New in version 2.1: The humanize
filter was added in Symfony 2.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 Translation Filters. |
text|transchoice(count, arguments = {}, domain = 'messages', locale = null) |
This will translate the text with pluralization, more information in Translation Filters. |
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) |
Tags¶
Tag Syntax | Usage |
---|---|
{% form_theme form 'file' %} |
This will look inside the given file for overridden form blocks, more information in How to customize Form Rendering. |
{% trans with {variables} %}...{% endtrans %} |
This will translate and render the text, more information in Twig Templates |
{% transchoice count with {variables} %}
...
{% endtranschoice %} |
This will translate and render the text with pluralization, more information in Twig Templates |
{% trans_default_domain language %} |
This will set the default domain for message catalogues in the current template |
Tests¶
New in version 2.1: The selectedchoice
test was added in Symfony 2.1
Test Syntax | Usage |
---|---|
selectedchoice(choice, selectedValue) |
This will return true if the choice is selected for the given form value |
Global Variables¶
Variable | Usage |
---|---|
app Attributes: app.user , app.request ,
app.session , app.environment , app.debug ,
app.security |
The app variable is available everywhere, and gives you quick
access to many commonly needed objects. The app variable is
instance of GlobalVariables |
Symfony Standard Edition Extensions¶
The Symfony Standard Edition adds some bundles to the Symfony2 Core Framework. Those bundles can have other Twig extensions:
- Twig Extensions includes some interesting extensions that do not belong to the Twig core. You can read more in the official Twig Extensions documentation;
- Assetic adds the
{% stylesheets %}
,{% javascripts %}
and{% image %}
tags. You can read more about them in the Assetic Documentation.
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.