PHP Twig模版的Filters详解(中文)

来源:互联网 发布:男士内裤品牌 知乎 编辑:程序博客网 时间:2024/05/16 18:52

目前支持的过滤器包括:

      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?
  1. {{ post.published_at|date("m/d/Y") }}  
  2. {{ "now"|date("m/d/Y") }}  
如果想在格式里输出字母,需要在每个字母前输入\\
[html]view plaincopyprint?
  1. {{ post.published_at|date("F jS \\a\\t g:ia") }}  
注意:经过我的测试,不能输入中文字符,这样写不行。。 {{ 'now'|date("F jS \\上\\午 g:ia") }}
你可以指定时区
[html]view plaincopyprint?
  1. {{ post.published_at|date("m/d/Y", "Europe/Paris") }}  
如果你提供的格式化字符串是不支持,会自动使用默认格式 (F j, Y H:i)你可以用代码修改这个默认格式
[php]view plaincopyprint?
  1. $twig = new Twig_Environment($loader);  
  2. $twig->getExtension('core')->setDateFormat('d/m/Y''%d days');  

format过滤器

和php的printf函数一样,用来替换占位符
[html]view plaincopyprint?
  1. {{ "I like %s and %s."|format(foo, "bar") }}  
  2.   
  3. {# returns I like foo and bar  
  4.    if the foo parameter equals to the foo string. #}  

replace过滤器

这个自己看吧
[html]view plaincopyprint?
  1. {{ "I like %this% and %that%."|replace({'%this%': foo, '%that%': "bar"}) }}  
  2.   
  3. {# returns I like foo and bar  
  4.    if the foo parameter equals to the foo string. #}  


number_format过滤器

1.5版本新增过滤器。
他是php函数 number_format的一个包装 直接见函数参考吧
[html]view plaincopyprint?
  1. {{ 200.35|number_format }}  
另外就是可以用php来修改默认的格式
[php]view plaincopyprint?
  1. $twig = new Twig_Environment($loader);  
  2. $twig->getExtension('core')->setNumberFormat(3, ',''.');  


url_encode过滤器

这个直接使用 urlencode函数
[html]view plaincopyprint?
  1. {{ data|url_encode() }}  

json_encode过滤器

直接使用json_encode函数
[html]view plaincopyprint?
  1. {{ data|json_encode() }}  

convert_encoding过滤器

1.4版本新加内容
转换一个字符串,第一个参数是输出编码,第二个参数是输入编码
本函数依赖于iconv 或者mbstring 所以至少需要安装一个
[html]view plaincopyprint?
  1. {{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}  

title过滤器

会让每个单词的首字母大写。
[html]view plaincopyprint?
  1. {{ 'my first car'|title }}  
  2.   
  3. {# outputs 'My First Car' #}  


capitalize过滤器

会把字符串变成 首字母大写,其余字母小写的格式
[html]view plaincopyprint?
  1. {{ 'my first car'|capitalize }}  
  2.   
  3. {# outputs 'My first car' #}  

nl2br过滤器

会把换行符\n 变成<br />
[html]view plaincopyprint?
  1. {{ "I like Twig.\nYou will like it too."|nl2br }}  
  2. {# outputs  
  3.   
  4.     I like Twig.<br />  
  5.     You will like it too.  
  6.   
  7. #}  

upper lower 过滤器

让字符串变大小写


striptags过滤器

直接使用的是strip_tags函数


join过滤器

这个我很喜欢,跟python的join一样,用来将一个数组的内容连接起来,并用指定的字符串分割。
[html]view plaincopyprint?
  1. {{ [1, 2, 3]|join }}  
  2. {# returns 123 #}  
  3. {{ [1, 2, 3]|join('|') }}  
  4. {# returns 1|2|3 #}  

reverse 过滤器

反转一个数组,或者是一个实现了Iterator接口的对象
[html]view plaincopyprint?
  1. {% for use in users|reverse %}  
  2.     ...  
  3. {% endfor %}  

length过滤器

返回一个数组或者字符串的长度
[html]view plaincopyprint?
  1. {% if users|length > 10 %}  
  2.     ...  
  3. {% endif %}  

sort过滤器

使用的是sort函数
[html]view plaincopyprint?
  1. {% for use in users|sort %}  
  2.     ...  
  3. {% endfor %}  
default过滤器
当变量没定义或者为空的时候,返回预先设置的内容
[html]view plaincopyprint?
  1. {{ var|default('var is not defined') }}  
  2.   
  3. {{ var.foo|default('foo item on var is not defined') }}  
  4.   
  5. {{ var['foo']|default('foo item on var is not defined') }}  
  6.   
  7. {{ ''|default('passed var is empty')  }}  

keys过滤器

返回key数组
[html]view plaincopyprint?
  1. {% for key in array|keys %}  
  2.     ...  
  3. {% endfor %}  

escape过滤器

主要转义  & < > ' " 。并且它有个简写方式 e。
[html]view plaincopyprint?
  1. {{ user.username|escape }}  
  2. {{ user.username|e }}  
还可以转义 js
[html]view plaincopyprint?
  1. {{ user.username|escape('js') }}  
  2. {{ user.username|e('js') }}  
实际上他使用的是php函数  htmlspecialchars

raw过滤器

用于在autoescape标签内部,标记出不需要转义的内容。
[html]view plaincopyprint?
  1. {% autoescape true %}  
  2.     {{ var|raw }} {# var won't be escaped #}  
  3. {% endautoescape %}  

merge过滤器

用来合并数组
[html]view plaincopyprint?
  1. {% set items = { 'apple': 'fruit', 'orange': 'fruit' } %}  
  2.   
  3. {% set items = items|merge({ 'peugeot': 'car' }) %}  
  4.   
  5. {# items now contains { 'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car' } #}  
0 0
原创粉丝点击