Twig's Filters Learning _php Tutorial

Source: Internet
Author: User
Tags uppercase letter
Currently supported filters include

Date format replace Number_format Url_encode Json_encode convert_encoding title capitalize nl2br Upper Lower striptags Joi n Reverse length Sort default keys escape raw Merge

The


Date Filter
1.1 has new time zone support, and the 1.5 version adds the default date format.
This filter is infinitely similar to PHP's date function
{{post.published_at|date ("m/d/y")}}
{"Now" |date ("M/d/y")}}
{{post.published _at|date ("m/d/y")}}
{{"Now" |date ("m/d/y")}
If you want to output letters in the format, you need to enter \ \
{post.published_at|date ("F JS \\a") before each letter. \\t G:ia ")}}
{{post.published_at|date (" F JS \\a\\t G:ia ")}} Note: After my test, you cannot enter Chinese characters, so write No. {' Now ' |date ("F jS \ \ \ 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 you provide a formatted string that is not supported, it will automatically use the default format (F J, Y h:i) You can modify this default format with code
$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
As with the printf function of PHP, to replace the placeholder
{"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
See for yourself {{"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 new filters added to the version.
He is the PHP function number_format a wrapper directly see the function reference bar
{{200.35|number_format}}
{{200.35|number_format}} It is also possible to modify the default format with PHP
$twig = new Twig_environment ($loader);
$twig->getextension (' core ')->setnumberformat (3, ', ', '. ');
$twig = new Twig_environment ($loader);
$twig->getextension (' core ')->setnumberformat (3, ', ', '. ');
Url_encode Filter
This directly uses the UrlEncode function
{{Data|url_encode ()}}
{{Data|url_encode ()}}


Json_encode Filter
Directly using the Json_encode function
{{Data|json_encode ()}}
{{Data|json_encode ()}}


Convert_encoding Filter
New additions to version 1.4
Convert a string, the first parameter is the output encoding, the second parameter is the input encoding
This function relies on iconv or mbstring so you need to install 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
Turns the string into an uppercase letter, and the remaining lowercase letters are formatted
{' My first car ' |capitalize}}

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

{# outputs ' My first car ' #}

NL2BR Filter
Will change the line character \ n into

{"I like twig.\nyou would like it too." | NL2BR}}
{# outputs

I like Twig.

You'll like it too.

#}
{"I like twig.\nyou would like it too." | NL2BR}}
{# outputs

I like Twig.

You'll like it too.

#}

Upper Lower Filter
Make a string uppercase and lowercase
Striptags Filter
The Strip_tags function is used directly
Join filter
I love that, like Python joins, to concatenate the contents of an array and split them with the 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
Reverses an array, or is 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
Using the Sort function
{% for use in users|sort%}
...
{% ENDFOR%}
{% for use in users|sort%}
...
{% ENDFOR%}
Default Filter
Returns the pre-set content when the variable is not defined or is empty
{{Var|default (' var is not defined ')}}

{{Var.foo|default (' foo item on var ' 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 ' 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 & < > ' ". And it has a shorthand way of 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 he's using a PHP function htmlspecialchars

Raw Filter
Used to mark content that does not need to be escaped inside the autoescape tag.
{% 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 '} #}

Excerpt from Jiaochangyun's column

http://www.bkjia.com/PHPjc/478458.html www.bkjia.com true http://www.bkjia.com/PHPjc/478458.html techarticle currently supported filters include date format replace Number_format Url_encode Json_encode convert_encoding title capitalize nl2br Upper Lowe R striptags Join Reverse length sort Defau ...

  • Related Article

    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.