Home >Backend Development >PHP Tutorial >Twig Filters Learning_PHP Tutorial

Twig Filters Learning_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:48:07890browse

Currently supported 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
Version 1.1 adds time zone support, and version 1.5 adds a 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 \
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, so writing this way will not work. . {{ 'now'|date("F jS \上\afternoon 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 provide is not supported, the default format (F j, Y H:i) will be automatically used. 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
Like PHP’s printf function, 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
See this 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
Version 1.5 adds new filters.
It is a wrapper for the PHP function number_format. Please refer to the function reference directly
{{ 200.35|number_format }}
{{ 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, ',', '.');
$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
Use the json_encode function directly
{{ data|json_encode() }}
{{ data|json_encode() }}


convert_encoding filter
New content added in version 1.4
Convert a string, the first parameter is the output encoding, the second parameter is the input encoding
This function depends on iconv or mbstring so at least one needs to be installed
{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}
{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}

title filter
Capitalizes the first letter of each word.
{{ 'my first car'|title }}

{# outputs 'My First Car' #}
{{ 'my first car'|title }}

{# outputs 'My First Car' #}
capitalize filter
The string will be formatted with the first letter in uppercase and the remaining letters in lowercase
{{ 'my first car'|capitalize }}

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

{# outputs 'My first car' #}

nl2br filter
will change the newline character n into

{{ "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
Make the string uppercase and lowercase
striptags filter
The strip_tags function is used directly
join filter
I like this very much. It is the same as Python's join. It is used to connect the contents of an array and split 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
Reverse 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
When the variable is undefined 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
Return key array
{% for key in array|keys %}
...
{% endfor %}
{% for key in array|keys %}
...
{% endfor %}

escape filter
The main escape is & < > ' ". And it has an abbreviation e.
{{ user.username|escape }}
{{ user.username|e }}
{{ user.username|escape }}
{{ user.username|e }} can also be escaped js
{{ user.username|escape('js') }}
{{ user.username|e('js') }}
{{ user.username|escape('js') }}
{{ user.username|e('js') }} Actually he is using the 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' } #}

Excerpted from jiaochangyun’s column

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478458.htmlTechArticleCurrently supported filters include date format replace number_format url_encode json_encode convert_encoding title capitalize nl2br upper lower striptags join reverse length sort defau ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn