Twig's Filters Learning

Source: Internet
Author: User

The filters currently supported 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 version 1.1 adds time zone support, and the 1.5 version increases the default date format. This filter and PHP's date function are infinitely similar

{{post.published_at|date (' m/d/y ')}
} {{' Now ' |date (' m/d/y ')}}

If you want to output letters in a format, you need to enter the letter before each letter \
{{post.published_at|date ("F JS \\a\\t G:ia")}}
Note: After my test, I can't enter Chinese characters, so I can't write. {{' Now ' |date ("F jS \ G:ia on \ Midday")}} You can specify the time zone
{{post.published_at|date ("m/d/y", "Europe/paris")}}
If you provide a format string that is not supported, it will automatically use the default format (F J, Y h:i) You can modify this default format in code
$twig = new Twig_environment ($loader);
$twig->getextension (' core ')->setdateformat (' d/m/y ', '%d days ');

Format FilterLike the printf function of PHP, to replace placeholders
{{"I like%s and%s." | Format (foo, bar)}

{# returns I like Foo and bar if the '
   parameter equals to the ' Foo string. #}

Replace filterLook at this yourself.
{{"I like%this% and%that%." | Replace ({'%this% ': foo, '%that% ': ' Bar '}}}

{# returns I like Foo and bar if the-
   parameter equals to the Foo String. #}
Number_format Filter1.5 version of the new filter. He is a PHP function number_format a package directly see the function reference bar
{{200.35|number_format}}}
In addition, you can use PHP to modify the default format
$twig = new Twig_environment ($loader);
$twig->getextension (' core ')->setnumberformat (3, ', ', '. ');
Url_encode FilterThis directly uses the UrlEncode function
{{Data|url_encode ()}}


Json_encode FilterUsing the Json_encode function directly
{{Data|json_encode ()}}


convert_encoding Filter1.4 Version The new content converts a string, the first parameter is the output encoding, and the second parameter is the input encoding this function relies on iconv or mbstring so at least one must be installed
{{data|convert_encoding (' UTF-8 ', ' ISO-2022-JP ')}}

Title FilterMakes the first letter of each word uppercase.
{{' My ' |title}}}

{# outputs ' my A ' #}
Capitalize filterTurns the string into a first-letter capitalization, the rest of the letter lowercase format
{{' My ' |capitalize}}}

{# outputs ' my A ' #}

NL2BR FilterWill change the line break into <br/>
{{"I like twig.\nyou would like it too." | NL2BR}
{# outputs I like twig.<br/> You'll like
    it too.

#}

Upper Lower FilterMake a string uppercase and lowercase striptags FilterThe Strip_tags function is used directly Join FilterI love this, like a python join, used to concatenate the contents of an array and divide it with the specified string.
{{[1, 2, 3]|join}}}
{# returns 123 #}
{{[1, 2, 3]|join (' | ')}}
{# returns 1|2|3 #}

Reverse FilterReverses an array, or an object that implements the iterator interface
{% for use in users|reverse%}
    ...
{% ENDFOR%}

Length FilterReturns the length of an array or string
{% if users|length >%}
    ...
{% ENDIF%}

Sort Filteris using the Sort function
{% for use in users|sort%}
    ...
{% ENDFOR%}

Default filter returns the preset content when the variable is undefined or empty
{{Var|default (' var is not defined ')}

} {{Var.foo|default (' Foo item on VAR are not defined ')}

} {{var[' foo ']|default (' foo item on VAR are not defined ')}

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

Keys FilterReturns the key array
{% for key in Array|keys%}
    ...
{% ENDFOR%}

Escape FilterMain Escape & < > ' ". And it has a shorthand way e.
{{User.username|escape}}}
{{user.username|e}}}
You can also escape JS
{{user.username|escape (' JS ')}}
{{user.username|e (' JS ')}}
In fact, he's using PHP functions Htmlspecialchars

Raw FilterUsed inside the autoescape tag to mark out content that does not need to be escaped.
{% Autoescape true%}
    {{Var|raw}}} {# var won ' t be escaped #}
{% Endautoescape%}

Merge FilterUsed to merge arrays
{% Set items = {' Apple ': ' Fruit ', ' orange ': ' Fruit '}%}

{% Set items = Items|merge ({' Peugeot ': ' car '})%}

{# items now contains {' Apple ': ' Fruit ', ' orange ': ' Fruit ', ' Peugeot ': ' Car '} #}


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.