目前支持的过滤器包括:
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');
和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. #}
这个自己看吧
[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, ',', '.');
这个直接使用 urlencode函数
[html]view plaincopyprint?
- {{ data|url_encode() }}
直接使用json_encode函数
[html]view plaincopyprint?
- {{ data|json_encode() }}
1.4版本新加内容
转换一个字符串,第一个参数是输出编码,第二个参数是输入编码
本函数依赖于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' #}
会把换行符\n 变成<br />
[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 #}
反转一个数组,或者是一个实现了Iterator接口的对象
[html]view plaincopyprint?
- {% for use in users|reverse %}
- ...
- {% endfor %}
返回一个数组或者字符串的长度
[html]view plaincopyprint?
- {% if users|length > 10 %}
- ...
- {% endif %}
使用的是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') }}
返回key数组
[html]view plaincopyprint?
- {% for key in array|keys %}
- ...
- {% endfor %}
主要转义 & < > ' " 。并且它有个简写方式 e。
[html]view plaincopyprint?
- {{ user.username|escape }}
- {{ user.username|e }}
还可以转义 js
[html]view plaincopyprint?
- {{ user.username|escape('js') }}
- {{ user.username|e('js') }}
实际上他使用的是php函数 htmlspecialchars
用于在autoescape标签内部,标记出不需要转义的内容。
[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' } #}