发布于 2015-08-27 16:36:52 | 313 次阅读 | 评论: 0 | 来源: 网络整理
Twig is the default template engine for Symfony. By itself, it already contains a lot of built-in functions, filters, tags and tests (learn more about them from the Twig Reference).
Symfony adds custom extensions on top of Twig to integrate some components into the Twig templates. The following sections describe the custom functions, filters, tags and tests that are available when using the Symfony Core Framework.
There may also be tags in bundles you use that aren’t listed here.
{{ render(uri, options) }}
uristring | ControllerReferenceoptionsarray default: []Renders the fragment for the given controller (using the controller function) or URI. For more information, see 嵌入Controller.
The render strategy can be specified in the strategy key of the options.
{{ render_esi(uri, options) }}
uristring | ControllerReferenceoptionsarray default: []Generates an ESI tag when possible or falls back to the behavior of render function instead. For more information, see 嵌入Controller.
小技巧
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.
{{ controller(controller, attributes, query) }}
controllerstringattributesarray default: []queryarray default: []Returns an instance of ControllerReference to be used with functions like render() and render_esi().
{{ asset(path, packageName, absolute = false, version = null) }}
pathstringpackageNamestring``|``null default: nullabsoluteboolean default: falseversionstring default nullReturns a public path to path, which takes into account the base path set for the package and the URL path. More information in 链接静态文件. For asset versioning, see assets_version.
{{ asset_version(packageName) }}
packageNamestring | null default: nullReturns the current version of the package, more information in 链接静态文件.
{{ form(view, variables) }}
viewFormViewvariablesarray default: []Renders the HTML of a complete form, more information in the Twig Form reference.
{{ form_start(view, variables) }}
viewFormViewvariablesarray default: []Renders the HTML start tag of a form, more information in the Twig Form reference.
{{ form_end(view, variables) }}
viewFormViewvariablesarray default: []Renders the HTML end tag of a form together with all fields that have not been rendered yet, more information in the Twig Form reference.
{{ form_enctype(view) }}
viewFormViewRenders the required enctype="multipart/form-data" attribute if the form contains at least one file upload field, more information in the Twig Form reference.
{{ form_widget(view, variables) }}
viewFormViewvariablesarray default: []Renders a complete form or a specific HTML widget of a field, more information in the Twig Form reference.
{{ form_errors(view) }}
viewFormViewRenders any errors for the given field or the global errors, more information in the Twig Form reference.
{{ form_label(view, label, variables) }}
viewFormViewlabelstring default: nullvariablesarray default: []Renders the label for the given field, more information in the Twig Form reference.
{{ form_row(view, variables) }}
viewFormViewvariablesarray default: []Renders the row (the field’s label, errors and widget) of the given field, more information in the Twig Form reference.
{{ form_rest(view, variables) }}
viewFormViewvariablesarray default: []Renders all fields that have not yet been rendered, more information in the Twig Form reference.
{{ csrf_token(intention) }}
intentionstringRenders a CSRF token. Use this function if you want CSRF protection without creating a form.
{{ is_granted(role, object, field) }}
rolestringobjectobjectfieldstringReturns true if the current user has the required role. Optionally, an object can be pasted to be used by the voter. More information can be found in 在模板中进行权限控制.
注解
You can also pass in the field to use ACE for a specific field. Read more about this in Scope of Access Control Entries.
{{ logout_path(key) }}
keystringGenerates a relative logout URL for the given firewall.
{{ logout_url(key) }}
keystringEqual to the logout_path function, but it’ll generate an absolute URL instead of a relative one.
{{ path(name, parameters, relative) }}
namestringparametersarray default: []relativeboolean default: falseReturns 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. More information in 创建链接.
{{ url(name, parameters, schemeRelative) }}
namestringparametersarray default: []schemeRelativeboolean default: falseReturns the absolute URL (with scheme and host) for the given route. If schemeRelative is enabled, it’ll create a scheme-relative URL. More information in 创建链接.
2.6 新版功能: The absolute_url function was introduced in Symfony 2.7
{{ absolute_url(path) }}
pathstringReturns the absolute URL for the given absolute path. This is useful to convert an existing path:
{{ absolute_url(asset(path)) }}
2.6 新版功能: The relative_path function was introduced in Symfony 2.7
{{ relative_path(path) }}
pathstringReturns a relative path for the given absolute path (based on the current request path). For instance, if the current path is /article/news/welcome.html, the relative path for /article/image.png is ../images.png.
Creates an Expression in Twig. See “Template Expressions”.
{{ text|humanize }}
textstringMakes a technical name human readable (i.e. replaces underscores by spaces and capitalizes the string).
{{ message|trans(arguments, domain, locale) }}
messagestringargumentsarray default: []domainstring default: nulllocalestring default: nullTranslates the text into the current language. More information in Translation Filters.
{{ message|transchoice(count, arguments, domain, locale) }}
messagestringcountintegerargumentsarray default: []domainstring default: nulllocalestring default: nullTranslates the text with pluralization support. More information in Translation Filters.
{{ input|yaml_encode(inline, dumpObjects) }}
inputmixedinlineinteger default: 0dumpObjectsboolean default: falseTransforms the input into YAML syntax. See Writing YAML Files for more information.
{{ value|yaml_dump(inline, dumpObjects) }}
valuemixedinlineinteger default: 0dumpObjectsboolean default: falseDoes the same as yaml_encode(), but includes the type in the output.
{{ class|abbr_class }}
classstringGenerates 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).
{{ method|abbr_method }}
methodstringGenerates 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()).
{{ args|format_args }}
argsarrayGenerates a string with the arguments and their types (within <em> elements).
{{ args|format_args_as_text }}
argsarrayEqual to the format_args filter, but without using HTML tags.
{{ file|file_excerpt(line) }}
filestringlineintegerGenerates an excerpt of seven lines around the given line.
{{ file|format_file(line, text) }}
filestringlineintegertextstring default: nullGenerates 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.root_dir (showing the full path in a tooltip on hover).
{{ text|format_file_from_text }}
textstringUses format_file to improve the output of default PHP errors.
{{ file|file_link(line) }}
lineintegerGenerates a link to the provided file (and optionally line number) using a preconfigured scheme.
{% if choice is selectedchoice(selectedValue) %}
choiceChoiceViewselectedValuestringChecks if selectedValue was checked for the provided choice field. Using this test is the most effective way.
The app variable is available everywhere and gives access to many commonly needed objects and values. It is an instance of GlobalVariables.
The available attributes are:
app.userapp.requestapp.sessionapp.environmentapp.debugapp.security2.6 新版功能: The app.security global is deprecated as of 2.6. The user is already available as app.user and is_granted() is registered as function.
The Symfony Standard Edition adds some bundles to the Symfony Core Framework. Those bundles can have other Twig extensions:
{% stylesheets %}, {% javascripts %} and {% image %} tags. You can read more about them in the Assetic Documentation.