Twig Extensions Defined by Symfony ¶
Twig is the template engine used in Symfony applications. There are tens of default filters and functions defined by Twig, but Symfony also defines some filters, functions and tags to integrate the various Symfony components with Twig templates. This article explains them all.
Tip
If these extensions provided by Symfony are not enough, you can create a custom Twig extension to define even more filters and functions.
Functions ¶
render ¶
1 |
{{ render(uri, options = []) }}
|
uri
-
type:
string
|ControllerReference
タイプ: 文字列 | ControllerReference options
(optional)-
type:
array
default:[]
タイプ: 配列 デフォルト: []
Makes a request to the given internal URI or controller and returns the result.
The render strategy can be specified in the strategy
key of the options.
It's commonly used to embed controllers in templates.
render_esi ¶
1 |
{{ render_esi(uri, options = []) }}
|
uri
-
type:
string
|ControllerReference
タイプ: 文字列 | ControllerReference options
(optional)-
type:
array
default:[]
タイプ: 配列 デフォルト: []
It's similar to the render function and defines the same arguments. However, it generates an ESI tag when ESI support is enabled or falls back to the behavior of render otherwise.
Tip
The render_esi()
function is an example of the shortcut functions
of render
. It automatically sets the strategy based on what's given
in the function name, e.g. render_hinclude()
will use the hinclude.js
strategy. This works for all render_*()
functions.
fragment_uri ¶
1 |
{{ fragment_uri(controller, absolute = false, strict = true, sign = true) }}
|
controller
-
type:
ControllerReference
タイプ: ControllerReference absolute
(optional)-
type:
boolean
default:false
タイプ: ブール デフォルト: false strict
(optional)-
type:
boolean
default:true
タイプ: ブール デフォルト: true sign
(optional)-
type:
boolean
default:true
タイプ: ブール デフォルト: true
Generates the URI of a fragment.
controller ¶
1 |
{{ controller(controller, attributes = [], query = []) }}
|
controller
-
type:
string
タイプ: 文字列 attributes
(optional)-
type:
array
default:[]
タイプ: 配列 デフォルト: [] query
(optional)-
type:
array
default:[]
タイプ: 配列 デフォルト: []
Returns an instance of ControllerReference
to be used with functions
like render() and
render_esi().
asset ¶
1 |
{{ asset(path, packageName = null) }}
|
path
-
type:
string
タイプ: 文字列 packageName
(optional)-
type:
string
|null
default:null
タイプ: 文字列 | null デフォルト: null
Returns the public path of the given asset path (which can be a CSS file, a JavaScript file, an image path, etc.). This function takes into account where the application is installed (e.g. in case the project is accessed in a host subdirectory) and the optional asset package base path.
Symfony provides various cache busting implementations via the Framework Configuration Reference (FrameworkBundle), Framework Configuration Reference (FrameworkBundle), and Framework Configuration Reference (FrameworkBundle) configuration options.
See also
Read more about linking to web assets from templates.
asset_version ¶
1 |
{{ asset_version(packageName = null) }}
|
packageName
(optional)-
type:
string
|null
default:null
タイプ: 文字列 | null デフォルト: null
Returns the current version of the package, more information in Creating and Using Templates.
csrf_token ¶
1 |
{{ csrf_token(intention) }}
|
intention
-
type:
string
- an arbitrary string used to identify the token.type: string - トークンを識別するために使用される任意の文字列。
Renders a CSRF token. Use this function if you want CSRF protection in a regular HTML form not managed by the Symfony Form component.
is_granted ¶
1 |
{{ is_granted(role, object = null, field = null) }}
|
role
-
type:
string
タイプ: 文字列 object
(optional)-
type:
object
タイプ: オブジェクト field
(optional)-
type:
string
タイプ: 文字列
Returns true
if the current user has the given role.
Optionally, an object can be passed to be used by the voter. More information can be found in Security.
logout_path ¶
1 |
{{ logout_path(key = null) }}
|
key
(optional)-
type:
string
タイプ: 文字列
Generates a relative logout URL for the given firewall. If no key is provided, the URL is generated for the current firewall the user is logged into.
logout_url ¶
1 |
{{ logout_url(key = null) }}
|
key
(optional)-
type:
string
タイプ: 文字列
Equal to the logout_path function, but it'll generate an absolute URL instead of a relative one.
path ¶
1 |
{{ path(route_name, route_parameters = [], relative = false) }}
|
name
-
type:
string
タイプ: 文字列 parameters
(optional)-
type:
array
default:[]
タイプ: 配列 デフォルト: [] relative
(optional)-
type:
boolean
default:false
タイプ: ブール デフォルト: false
Returns the relative URL (without the scheme and host) for the given route.
If relative
is enabled, it'll create a path relative to the current path.
See also
Read more about Symfony routing and about creating links in Twig templates.
url ¶
1 |
{{ url(route_name, route_parameters = [], schemeRelative = false) }}
|
name
-
type:
string
タイプ: 文字列 parameters
(optional)-
type:
array
default:[]
タイプ: 配列 デフォルト: [] schemeRelative
(optional)-
type:
boolean
default:false
タイプ: ブール デフォルト: false
Returns the absolute URL (with scheme and host) for the given route. If
schemeRelative
is enabled, it'll create a scheme-relative URL.
See also
Read more about Symfony routing and about creating links in Twig templates.
absolute_url ¶
1 |
{{ absolute_url(path) }}
|
path
-
type:
string
タイプ: 文字列
Returns the absolute URL (with scheme and host) from the passed relative path. Combine it with the asset() function to generate absolute URLs for web assets. Read more about Linking to CSS, JavaScript and Image Assets.
relative_path ¶
1 |
{{ relative_path(path) }}
|
path
-
type:
string
タイプ: 文字列
Returns the relative path from the passed absolute URL. For example, assume
you're on the following page in your app:
http://example.com/products/hover-board
.
1 2 3 4 5 |
{{ relative_path('http://example.com/human.txt') }}
{# ../human.txt #}
{{ relative_path('http://example.com/products/products_icon.png') }}
{# products_icon.png #}
|
expression ¶
Creates an Expression related to the ExpressionLanguage component.
impersonation_exit_path ¶
1 |
{{ impersonation_exit_path(exitTo = null) }}
|
exitTo
(optional)-
type:
string
タイプ: 文字列
Generates a URL that you can visit to exit user impersonation.
After exiting impersonation, the user is redirected to the current URI. If you
prefer to redirect to a different URI, define its value in the exitTo
argument.
If no user is being impersonated, the function returns an empty string.
impersonation_exit_url ¶
1 |
{{ impersonation_exit_url(exitTo = null) }}
|
exitTo
(optional)-
type:
string
タイプ: 文字列
It's similar to the impersonation_exit_path function, but it generates absolute URLs instead of relative URLs.
t
1 |
{{ t(message, parameters = [], domain = 'messages')|trans }}
|
message
-
type:
string
タイプ: 文字列 parameters
(optional)-
type:
array
default:[]
タイプ: 配列 デフォルト: [] domain
(optional)-
type:
string
default:messages
タイプ: 文字列 デフォルト: メッセージ
Creates a Translatable
object that can be passed to the
trans filter.
Form Related Functions ¶
The following functions related to Symfony Forms are also available. They are explained in the article about customizing form rendering:
- form()形()
- form_start()form_start()
- form_end()フォームエンド()
- form_widget()フォームウィジェット()
- form_errors()form_errors()
- form_label()フォームラベル()
- form_help()form_help()
- form_row()フォーム行()
- form_rest()フォームレスト()
Filters ¶
humanize ¶
1 |
{{ text|humanize }}
|
text
-
type:
string
タイプ: 文字列
Makes a technical name human readable (i.e. replaces underscores by spaces
or transforms camelCase text like helloWorld
to hello world
and then capitalizes the string).
trans ¶
1 |
{{ message|trans(arguments = [], domain = null, locale = null) }}
|
message
-
type:
string
|Translatable
タイプ: 文字列 |翻訳可能 arguments
(optional)-
type:
array
default:[]
タイプ: 配列 デフォルト: [] domain
(optional)-
type:
string
default:null
タイプ: 文字列 デフォルト: null locale
(optional)-
type:
string
default:null
タイプ: 文字列 デフォルト: null
Translates the text into the current language. More information in Translation Filters.
sanitize_html ¶
6.1
The sanitize_html()
filter was introduced in Symfony 6.1.
1 |
{{ body|sanitize_html(sanitizer = "default") }}
|
body
-
type:
string
タイプ: 文字列 sanitizer
(optional)-
type:
string
default:"default"
タイプ: 文字列 デフォルト: "デフォルト"
Sanitizes the text using the HTML Sanitizer component. More information in HTML Sanitizer.
yaml_encode ¶
1 |
{{ input|yaml_encode(inline = 0, dumpObjects = false) }}
|
input
-
type:
mixed
タイプ: 混合 inline
(optional)-
type:
integer
default:0
タイプ: 整数 デフォルト: 0 dumpObjects
(optional)-
type:
boolean
default:false
タイプ: ブール デフォルト: false
Transforms the input into YAML syntax. See The Yaml Component for more information.
yaml_dump ¶
1 |
{{ value|yaml_dump(inline = 0, dumpObjects = false) }}
|
value
-
type:
mixed
タイプ: 混合 inline
(optional)-
type:
integer
default:0
タイプ: 整数 デフォルト: 0 dumpObjects
(optional)-
type:
boolean
default:false
タイプ: ブール デフォルト: false
Does the same as yaml_encode(), but includes the type in the output.
abbr_class ¶
1 |
{{ class|abbr_class }}
|
class
-
type:
string
タイプ: 文字列
Generates an <abbr>
element with the short name of a PHP class (the
FQCN will be shown in a tooltip when a user hovers over the element).
abbr_method ¶
1 |
{{ method|abbr_method }}
|
method
-
type:
string
タイプ: 文字列
Generates an <abbr>
element using the FQCN::method()
syntax. If
method
is Closure
, Closure
will be used instead and if method
doesn't have a class name, it's shown as a function (method()
).
format_args ¶
1 |
{{ args|format_args }}
|
args
-
type:
array
タイプ: 配列
Generates a string with the arguments and their types (within <em>
elements).
format_args_as_text ¶
1 |
{{ args|format_args_as_text }}
|
args
-
type:
array
タイプ: 配列
Equal to the format_args filter, but without using HTML tags.
file_excerpt ¶
1 |
{{ file|file_excerpt(line, srcContext = 3) }}
|
file
-
type:
string
タイプ: 文字列 line
-
type:
integer
タイプ: 整数 srcContext
(optional)-
type:
integer
タイプ: 整数
Generates an excerpt of a code file around the given line
number. The
srcContext
argument defines the total number of lines to display around the
given line number (use -1
to display the whole file).
format_file ¶
1 |
{{ file|format_file(line, text = null) }}
|
file
-
type:
string
タイプ: 文字列 line
-
type:
integer
タイプ: 整数 text
(optional)-
type:
string
default:null
タイプ: 文字列 デフォルト: null
Generates the file path inside an <a>
element. If the path is inside
the kernel root directory, the kernel root directory path is replaced by
kernel.project_dir
(showing the full path in a tooltip on hover).
format_file_from_text ¶
1 |
{{ text|format_file_from_text }}
|
text
-
type:
string
タイプ: 文字列
Uses format_file to improve the output of default PHP errors.
file_link ¶
1 |
{{ file|file_link(line) }}
|
file
-
type:
string
タイプ: 文字列 line
-
type:
integer
タイプ: 整数
Generates a link to the provided file and line number using a preconfigured scheme.
file_relative ¶
1 |
{{ file|file_relative }}
|
file
-
type:
string
タイプ: 文字列
It transforms the given absolute file path into a new file path relative to project's root directory:
1 2 |
{{ '/var/www/blog/templates/admin/index.html.twig'|file_relative }}
{# if project root dir is '/var/www/blog/', it returns 'templates/admin/index.html.twig' #}
|
If the given file path is out of the project directory, a null
value
will be returned.
serialize ¶
1 |
{{ object|serialize(format = 'json', context = []) }}
|
object
-
type:
mixed
タイプ: 混合 format
(optional)-
type:
string
タイプ: 文字列 context
(optional)-
type:
array
タイプ: 配列
Accepts any data that can be serialized by the Serializer component
and returns a serialized string in the specified format
.
Tags ¶
form_theme ¶
1 |
{% form_theme form resources %}
|
form
-
type:
FormView
タイプ: FormView resources
-
type:
array
|string
タイプ: 配列 |ストリング
Sets the resources to override the form theme for the given form view instance.
You can use _self
as resources to set it to the current resource. More
information in How to Customize Form Rendering.
trans ¶
1 |
{% trans with vars from domain into locale %}{% endtrans %}
|
vars
(optional)-
type:
array
default:[]
タイプ: 配列 デフォルト: [] domain
(optional)-
type:
string
default:string
タイプ: 文字列 デフォルト: 文字列 locale
(optional)-
type:
string
default:string
タイプ: 文字列 デフォルト: 文字列
Renders the translation of the content. More information in Translations.
trans_default_domain ¶
1 |
{% trans_default_domain domain %}
|
domain
-
type:
string
タイプ: 文字列
This will set the default domain in the current template.
stopwatch ¶
1 |
{% stopwatch 'event_name' %}...{% endstopwatch %}
|
This measures the time and memory used to execute some code in the template and displays it in the Symfony profiler. See how to profile Symfony applications.
Tests ¶
The following tests related to Symfony Forms are available. They are explained in the article about customizing form rendering:
- selectedchoice()選択された選択肢()
- rootform()rootform()
Global Variables ¶
app ¶
The app
variable is injected automatically by Symfony in all templates and
provides access to lots of useful application information. Read more about the
Twig global app variable.