Twig's Filters learning _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Twig Filters learning. Currently, the supported filters include filters currently supported by filters include

Date format replace number_format url_encode json_encode convert_encoding title capitalize nl2br upper lower striptags join reverse length sort default keys escape raw merge


Date filter
The time zone is supported in version 1.1 and the default date format is added in version 1.5.
This filter is infinitely similar to the date function of php.
{Post. published_at | date ("m/d/Y ")}}
{"Now" | date ("m/d/Y ")}}
{Post. published_at | date ("m/d/Y ")}}
{"Now" | date ("m/d/Y ")}}
To output letters in the format, enter \ before each letter \\
{Post. published_at | date ("F jS \ a \ t g: ia ")}}
{Post. published_at | date ("F jS \ a \ t g: ia")} note: After my test, Chinese characters cannot be entered .. {'Now '| date ("F jS \ Shang \ Wu g: ia ")}}
You can specify the time zone
{Post. published_at | date ("m/d/Y", "Europe/Paris ")}}
{Post. published_at | date ("m/d/Y", "Europe/Paris ")}}
If the format string you provided is not supported, the default format (F j, y h: I) is automatically used. you can use the code to modify the default format.
$ Twig = new Twig_Environment ($ loader );
$ Twig-> getExtension ('core')-> setDateFormat ('d/m/Y', '% d days ');
$ Twig = new Twig_Environment ($ loader );
$ Twig-> getExtension ('core')-> setDateFormat ('d/m/Y', '% d days ');

Format filter
Like the printf function of php, it is used to replace placeholders.
{"I like % s and % s." | format (foo, "bar ")}}

{# Returns I like foo and bar
If the foo parameter equals to the foo string .#}
{"I like % s and % s." | format (foo, "bar ")}}

{# Returns I like foo and bar
If the foo parameter equals to the foo string .#}

Replace filter
Let's take a look at {"I like % this % and % that %. "| replace ({'% this %': foo, '% that %':" bar "})}}

{# Returns I like foo and bar
If the foo parameter equals to the foo string .#}
{"I like % this % and % that %." | replace ({'% this %': foo, '% that %': "bar "})}}

{# Returns I like foo and bar
If the foo parameter equals to the foo string .#}
Number_format filter
1.5 adds a filter.
He is a pack of the php function number_format. For more information, see function reference.
{200.35 | number_format }}
{200.35 | number_format}. you can use php to modify the default format.
$ Twig = new Twig_Environment ($ loader );
$ Twig-> getExtension ('core')-> setNumberFormat (3 ,',','.');
$ Twig = new Twig_Environment ($ loader );
$ Twig-> getExtension ('core')-> setNumberFormat (3 ,',','.');
Url_encode filter
Use the urlencode function directly.
{Data | url_encode ()}}
{Data | url_encode ()}}


Json_encode filter
Use the json_encode function directly
{Data | json_encode ()}}
{Data | json_encode ()}}


Convert_encoding filter
New content of version 1.4
Converts a string. The first parameter is output encoding, and the second parameter is input encoding.
This function depends on iconv or mbstring, so at least one
{Data | convert_encoding ('utf-8', 'ISO-2022-jp ')}}
{Data | convert_encoding ('utf-8', 'ISO-2022-jp ')}}

Title filter
The first letter of each word is capitalized.
{'My first car' | title }}

{# Outputs 'My First car '#}
{'My first car' | title }}

{# Outputs 'My First car '#}
Capitalize filter
Converts the string to the upper-case letter and the lower-case letter format.
{'My first car' | capitalize }}

{# Outputs 'My first car '#}
{'My first car' | capitalize }}

{# Outputs 'My first car '#}

Nl2br filter
The line break \ n is changed

{"I like Twig. \ nYou will like it too." | nl2br }}
{# Outputs

I like Twig.

You will like it too.

#}
{"I like Twig. \ nYou will like it too." | nl2br }}
{# Outputs

I like Twig.

You will like it too.

#}

Upper lower filter
Case-insensitive string
Striptags filter
The strip_tags function is used directly.
Join filter
I like this very much. like python's join, it is used to connect the content of an array and separate it with a specified string.
{[1, 2, 3] | join }}
{# Returns 123 #}
{[1, 2, 3] | join ('| ')}}
{# Returns 1 | 2 | 3 #}
{[1, 2, 3] | join }}
{# Returns 123 #}
{[1, 2, 3] | join ('| ')}}
{# Returns 1 | 2 | 3 #}

Reverse filter
Returns an array or an object that implements the Iterator interface.
{% For use in users | reverse %}
...
{% Endfor %}
{% For use in users | reverse %}
...
{% Endfor %}

Length filter
Returns the length of an array or string.
|%If users | length> 10%}
...
{% Endif %}
|%If users | length> 10%}
...
{% Endif %}

Sort filter
The sort function is used.
{% For use in users | sort %}
...
{% Endfor %}
{% For use in users | sort %}
...
{% Endfor %}
Default filter
If the variable is not defined or empty, the preset content is returned.
{Var | default ('Var is not defined ')}}

{Var. foo | default ('foo item on var is not defined ')}}

{Var ['foo'] | default ('foo item on var is not defined ')}}

{''| Default ('passed var is empty ')}}
{Var | default ('Var is not defined ')}}

{Var. foo | default ('foo item on var is not defined ')}}

{Var ['foo'] | default ('foo item on var is not defined ')}}

{''| Default ('passed var is empty ')}}

Keys filter
Returns the key array.
{% For key in array | keys %}
...
{% Endfor %}
{% For key in array | keys %}
...
{% Endfor %}

Escape filter
Main Escape & <> '". In addition, it has an abbreviated form e.
{User. username | escape }}
{User. username | e }}
{User. username | escape }}
{User. username | e} can also escape js
{User. username | escape ('js ')}}
{User. username | e ('js ')}}
{User. username | escape ('js ')}}
{User. username | e ('js')} actually uses the php function htmlspecialchars.

Raw filter
Used to mark the content that does not need to be escaped within the autoescape label.
{% Autoescape true %}
{Var | raw }{# var won't be escaped #}
{% Endautoescape %}
{% Autoescape true %}
{Var | raw }{# var won't be escaped #}
{% Endautoescape %}

Merge filter
Used to merge arrays
{% Set items = {'apple': 'fruit', 'Orange ': 'fruit'} %}

{% Set itemsitems = items | merge ({'peugeot': 'car'}) %}

{# Items now contains {'apple': 'fruit', 'Orange ': 'fruit', 'peugeot': 'car '}#}

From the column jiaochangyun

Raise date format replace number_format url_encode json_encode convert_encoding title capitalize nl2br upper lower striptags join reverse length sort defau...

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.