PHP Twig模版的Filters详解(中文)
2015-10-29 16:42
671 查看
目前支持的过滤器包括:
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
1.1版本新增时区支持,1.5版本增加了默认的日期格式。
这个过滤器和php的date函数无限类似
[html]view
plaincopyprint?
{{ post.published_at|date("m/d/Y") }}
{{ "now"|date("m/d/Y") }}
如果想在格式里输出字母,需要在每个字母前输入\\
[html]view
plaincopyprint?
{{ post.published_at|date("F jS \\a\\t g:ia") }}
注意:经过我的测试,不能输入中文字符,这样写不行。。 {{ 'now'|date("F jS \\上\\午 g:ia") }}
你可以指定时区
[html]view
plaincopyprint?
{{ post.published_at|date("m/d/Y", "Europe/Paris") }}
如果你提供的格式化字符串是不支持,会自动使用默认格式 (F j, Y H:i)你可以用代码修改这个默认格式
[php]view
plaincopyprint?
$twig = new Twig_Environment($loader);
$twig->getExtension('core')->setDateFormat('d/m/Y', '%d days');
[html]view
plaincopyprint?
{{ "I like %s and %s."|format(foo, "bar") }}
{# returns I like foo and bar
if the foo parameter equals to the foo string. #}
[html]view
plaincopyprint?
{{ "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. #}
1.5版本新增过滤器。
他是php函数 number_format的一个包装 直接见函数参考吧
[html]view
plaincopyprint?
{{ 200.35|number_format }}
另外就是可以用php来修改默认的格式
[php]view
plaincopyprint?
$twig = new Twig_Environment($loader);
$twig->getExtension('core')->setNumberFormat(3, ',', '.');
[html]view
plaincopyprint?
{{ data|url_encode() }}
[html]view
plaincopyprint?
{{ data|json_encode() }}
转换一个字符串,第一个参数是输出编码,第二个参数是输入编码
本函数依赖于iconv 或者mbstring 所以至少需要安装一个
[html]view
plaincopyprint?
{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}
[html]view
plaincopyprint?
{{ 'my first car'|title }}
{# outputs 'My First Car' #}
[html]view
plaincopyprint?
{{ 'my first car'|capitalize }}
{# outputs 'My first car' #}
[html]view
plaincopyprint?
{{ "I like Twig.\nYou will like it too."|nl2br }}
{# outputs
I like Twig.<br />
You will like it too.
#}
直接使用的是strip_tags函数
这个我很喜欢,跟python的join一样,用来将一个数组的内容连接起来,并用指定的字符串分割。
[html]view
plaincopyprint?
{{ [1, 2, 3]|join }}
{# returns 123 #}
{{ [1, 2, 3]|join('|') }}
{# returns 1|2|3 #}
[html]view
plaincopyprint?
{% for use in users|reverse %}
...
{% endfor %}
[html]view
plaincopyprint?
{% if users|length > 10 %}
...
{% endif %}
[html]view
plaincopyprint?
{% for use in users|sort %}
...
{% endfor %}
default过滤器
当变量没定义或者为空的时候,返回预先设置的内容
[html]view
plaincopyprint?
{{ 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') }}
[html]view
plaincopyprint?
{% for key in array|keys %}
...
{% endfor %}
[html]view
plaincopyprint?
{{ user.username|escape }}
{{ user.username|e }}
还可以转义 js
[html]view
plaincopyprint?
{{ user.username|escape('js') }}
{{ user.username|e('js') }}
实际上他使用的是php函数 htmlspecialchars
[html]view
plaincopyprint?
{% autoescape true %}
{{ var|raw }} {# var won't be escaped #}
{% endautoescape %}
[html]view
plaincopyprint?
{% set items = { 'apple': 'fruit', 'orange': 'fruit' } %}
{% set items = items|merge({ 'peugeot': 'car' }) %}
{# items now contains { 'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car' } #}
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过滤器
1.1版本新增时区支持,1.5版本增加了默认的日期格式。这个过滤器和php的date函数无限类似
[html]view
plaincopyprint?
{{ post.published_at|date("m/d/Y") }}
{{ "now"|date("m/d/Y") }}
如果想在格式里输出字母,需要在每个字母前输入\\
[html]view
plaincopyprint?
{{ post.published_at|date("F jS \\a\\t g:ia") }}
注意:经过我的测试,不能输入中文字符,这样写不行。。 {{ 'now'|date("F jS \\上\\午 g:ia") }}
你可以指定时区
[html]view
plaincopyprint?
{{ post.published_at|date("m/d/Y", "Europe/Paris") }}
如果你提供的格式化字符串是不支持,会自动使用默认格式 (F j, Y H:i)你可以用代码修改这个默认格式
[php]view
plaincopyprint?
$twig = new Twig_Environment($loader);
$twig->getExtension('core')->setDateFormat('d/m/Y', '%d days');
format过滤器
和php的printf函数一样,用来替换占位符[html]view
plaincopyprint?
{{ "I like %s and %s."|format(foo, "bar") }}
{# returns I like foo and bar
if the foo parameter equals to the foo string. #}
replace过滤器
这个自己看吧[html]view
plaincopyprint?
{{ "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过滤器
1.5版本新增过滤器。他是php函数 number_format的一个包装 直接见函数参考吧
[html]view
plaincopyprint?
{{ 200.35|number_format }}
另外就是可以用php来修改默认的格式
[php]view
plaincopyprint?
$twig = new Twig_Environment($loader);
$twig->getExtension('core')->setNumberFormat(3, ',', '.');
url_encode过滤器
这个直接使用 urlencode函数[html]view
plaincopyprint?
{{ data|url_encode() }}
json_encode过滤器
直接使用json_encode函数[html]view
plaincopyprint?
{{ data|json_encode() }}
convert_encoding过滤器
1.4版本新加内容转换一个字符串,第一个参数是输出编码,第二个参数是输入编码
本函数依赖于iconv 或者mbstring 所以至少需要安装一个
[html]view
plaincopyprint?
{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}
title过滤器
会让每个单词的首字母大写。[html]view
plaincopyprint?
{{ 'my first car'|title }}
{# outputs 'My First Car' #}
capitalize过滤器
会把字符串变成 首字母大写,其余字母小写的格式[html]view
plaincopyprint?
{{ 'my first car'|capitalize }}
{# outputs 'My first car' #}
nl2br过滤器
会把换行符\n 变成<br />[html]view
plaincopyprint?
{{ "I like Twig.\nYou will like it too."|nl2br }}
{# outputs
I like Twig.<br />
You will like it too.
#}
upper lower 过滤器
让字符串变大小写
striptags过滤器
直接使用的是strip_tags函数
join过滤器
这个我很喜欢,跟python的join一样,用来将一个数组的内容连接起来,并用指定的字符串分割。[html]view
plaincopyprint?
{{ [1, 2, 3]|join }}
{# returns 123 #}
{{ [1, 2, 3]|join('|') }}
{# returns 1|2|3 #}
reverse 过滤器
反转一个数组,或者是一个实现了Iterator接口的对象[html]view
plaincopyprint?
{% for use in users|reverse %}
...
{% endfor %}
length过滤器
返回一个数组或者字符串的长度[html]view
plaincopyprint?
{% if users|length > 10 %}
...
{% endif %}
sort过滤器
使用的是sort函数[html]view
plaincopyprint?
{% for use in users|sort %}
...
{% endfor %}
default过滤器
当变量没定义或者为空的时候,返回预先设置的内容
[html]view
plaincopyprint?
{{ 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过滤器
返回key数组[html]view
plaincopyprint?
{% for key in array|keys %}
...
{% endfor %}
escape过滤器
主要转义 & < > ' " 。并且它有个简写方式 e。[html]view
plaincopyprint?
{{ user.username|escape }}
{{ user.username|e }}
还可以转义 js
[html]view
plaincopyprint?
{{ user.username|escape('js') }}
{{ user.username|e('js') }}
实际上他使用的是php函数 htmlspecialchars
raw过滤器
用于在autoescape标签内部,标记出不需要转义的内容。[html]view
plaincopyprint?
{% autoescape true %}
{{ var|raw }} {# var won't be escaped #}
{% endautoescape %}
merge过滤器
用来合并数组[html]view
plaincopyprint?
{% set items = { 'apple': 'fruit', 'orange': 'fruit' } %}
{% set items = items|merge({ 'peugeot': 'car' }) %}
{# items now contains { 'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car' } #}
相关文章推荐
- 探索PowerShell(十二) 筛选器 Filters
- Twig模版引擎简介:基本API用法 - Twig使用指南 3ff0
- 安装Twig模版引擎-Twig使用指南
- 为开发者而生的Twig(上)-Twig使用指南 3ff0
- tomcat容器提供的过滤器Filters【译】
- ThinkPHP开发必备composer扩展包
- 内联过滤器Inline Filters
- ThinkPHP开发必备composer扩展包
- 使用twig来组装数据使数据结构可视化
- PHP Twig模版的tags详解(中文)
- 关于Kernelized Correlation Filters(KCF)运行中的小问题
- 【探索PowerShell 】【十二】筛选器 - Filters
- 在 F5 LTM 上配置数据包过滤 推荐
- symfony中自定模板及调用方式
- Image Filters for IOS
- iOS Image Filters
- 自定义django模板的 tags和filters
- Twig的安装与使用
- 抓包工具Fiddler的使用教程(十八): Fiddler中如何过滤会话
- twig 之基本语法