Template filter for Twig in Symfony

Source: Internet
Author: User

Filter Filters

Variables can be modified by filters. Filters and variables are separated by (|). Filters can also have parameters. Filters can also be used in multiple uses.

Universal FilterDate Filter

Version 1.1 adds time zone support, and version 1.5 has the default date format.
Format time, this filter is infinitely similar to PHP's date function, can handle strtotime compatible strings, or datetime/dateinterval instances, optional second parameter is used to specify the time zone, if the decorated data is empty, the current time is assumed to be the default

{{ article.publishedTime|date(‘Y-m-d H:i:s’) }}

Output: 2014-7-12 12:11:11

Format Filter

As with the printf function of PHP, to replace the placeholder

<span class=”days”>    {{ “%02d”|format(countdown.days) }}</span>天

Output: 12 days

Replace filter
{{ “教师可在直播开始前%hour%小时内进入直播教室,以准备相关教学资料.”    |replace({‘%hour%’: hour}) }}

Output: Teachers can enter the live classroom within 1 hours of the broadcast to prepare relevant teaching materials.

Number_format Filter

He is the PHP function number_format a wrapper directly see Function reference PHP function Number_format Bar

<span class=”rating-num”>    {{ course.rating|number_format(1) }}分</span>

Output: 5 points

Url_encode Filter

Encoded link string, equivalent to PHP function UrlEncode

{{ data|url_encode() }}
Json_encode Filter

Encoded JSON format, equivalent to PHP function Json_encode

{{ data|json_encode() }}
convert_encoding Filter

Encoding conversion, the first parameter specifies the converted encoding, the second parameter specifies the encoding before the conversion, the function relies on iconv or mbstring so at least one must be installed

{{ data|convert_encoding(‘UTF-8’, ‘iso-2022-jp’) }}
Title Filter

Capitalize the first letter of each word in a string, equivalent to Ucwords

{{ ‘my first car’|title }}

Output: My First Car

Capitalize filter

Capitalize the first letter of the string, and the remainder of the letter in lowercase format, equivalent to Ucfirst

{{ ‘my first car’|capitalize }}

Output: My first car

NL2BR Filter

Replace \ n in string with <br/>

{{ “I like Twig.\nYou will like it too.”|nl2br }}

Output:
I like Twig. <br/>
You'll like it too.

Join Filter

Composes the individual elements of an array into a string by a specified delimiter

{{ [1, 2, 3]|join }}

Output: 123

{{ [1, 2, 3]|join(‘|’) }}

Output: 1|2|3

Reverse Filter

Reverses an array, or an object that implements the iterator interface, and adds the processing of the string on the basis of the Array_reverse

{% for use in users|reverse %}      …  {% endfor %}
Length Filter

Returns the length of an array or string, equivalent to the combination of count and strlen

{% if users|length > 10 %}      …  {% endif %}
Sort Filter

Sort an array

{% for use in users|sort %}      …  {% endfor %}
Keys Filter

Extracts all the key names of an array into an array, equivalent to Array_keys

{% for key in array|keys %}      …{% endfor %}
Escape Filter

The string is securely processed into legitimate specified data, supports multiple conversion modes, default mode is HTML, and other selectable modes are html_attr, JS, CSS, URL, main escape & < > ' ". And it has a shorthand way of E.

{{ user.username|escape }}  {{ user.username|e }}
Raw Filter

Used to mark content that does not need to be escaped inside the autoescape tag.

<div class=”thread-body”>    {{article.body |raw }}</div>
Merge Filter

Used to combine arrays, similar to Array_merge. such as {{array 1|merge (array 2)}}

{% set items = { ‘id’:course.id} %}{% set items = items|merge({type:’all’}) %}

Template filter for Twig in Symfony

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.