Symfony2 Twig Extensions
Edit this pageWarning: You are browsing the documentation for Symfony 2.1, which is no longer maintained.
Read the updated version of this page for Symfony 6.0 (the current stable version).
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
2.1
The csrf_token
, logout_path
and logout_url
functions were added in Symfony2.1
Function Syntax | Usage |
---|---|
asset(path, packageName = null) |
Get the public path of the asset, more information in "Creating and using Templates". |
asset_version(packageName = null) |
Get the current version of the package, more information in "Creating and using Templates". |
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 "Security" |
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 "Creating and using Templates". |
url(name, parameters = {}) |
Equal to path(...) but it generates an absolute url |
Filters
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 . 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 |
---|---|
{% render url('route', {parameters}) %} |
This will render the Response Content for the given controller that the URL points to. For more information, see Creating and using Templates. |
{% 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 Translations |
{% transchoice count with {variables} %}
...
{% endtranschoice %} |
This will translate and render the text with pluralization, more information in Translations |
{% trans_default_domain language %} |
This will set the default domain for message catalogues in the current template |
Tests
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 |
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 Extension includes all extensions that do not belong to the
Twig core but can be interesting. 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;