zoukankan      html  css  js  c++  java
  • Twig 的Filters学习

    目前支持的过滤器包括

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

    format过滤器

    和php的printf函数一样,用来替换占位符
    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. #} 
    {{ "I like %s and %s."|format(foo, "bar") }}
    
    {# returns I like foo and bar
       if the foo parameter equals to the foo string. #}

    replace过滤器

    这个自己看吧
    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. #} 
    {{ "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的一个包装 直接见函数参考吧
    1. {{ 200.35|number_format }} 
    {{ 200.35|number_format }}
    另外就是可以用php来修改默认的格式
    1. $twig = new Twig_Environment($loader); 
    2. $twig->getExtension('core')->setNumberFormat(3, ',', '.'); 
    $twig = new Twig_Environment($loader);
    $twig->getExtension('core')->setNumberFormat(3, ',', '.');

    url_encode过滤器

    这个直接使用 urlencode函数
    1. {{ data|url_encode() }} 
    {{ data|url_encode() }}

    json_encode过滤器

    直接使用json_encode函数
    1. {{ data|json_encode() }} 
    {{ data|json_encode() }}

    convert_encoding过滤器

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

    title过滤器

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

    capitalize过滤器

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

    nl2br过滤器

    会把换行符\n 变成<br />
    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. #} 
    {{ "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一样,用来将一个数组的内容连接起来,并用指定的字符串分割。
    1. {{ [1, 2, 3]|join }} 
    2. {# returns 123 #} 
    3. {{ [1, 2, 3]|join('|') }} 
    4. {# returns 1|2|3 #} 
    {{ [1, 2, 3]|join }}
    {# returns 123 #}
    {{ [1, 2, 3]|join('|') }}
    {# returns 1|2|3 #}

    reverse 过滤器

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

    length过滤器

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

    sort过滤器

    使用的是sort函数
    1. {% for use in users|sort %} 
    2.     ... 
    3. {% endfor %} 
    {% for use in users|sort %}
        ...
    {% endfor %}
    default过滤器
    当变量没定义或者为空的时候,返回预先设置的内容
    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')  }} 
    {{ 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数组
    1. {% for key in array|keys %} 
    2.     ... 
    3. {% endfor %} 
    {% for key in array|keys %}
        ...
    {% endfor %}

    escape过滤器

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

    raw过滤器

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

    merge过滤器

    用来合并数组
    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' } #} 
    {% set items = { 'apple': 'fruit', 'orange': 'fruit' } %}
    
    {% set items = items|merge({ 'peugeot': 'car' }) %}
    
    {# items now contains { 'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car' } #}
  • 相关阅读:
    大数据
    优化
    gnu
    sed
    hadoop部署
    安装 zookeeper
    ansible
    ssh 配置无密码登录
    未完待续
    解正向代理、反向代理、透明代理
  • 原文地址:https://www.cnblogs.com/Kakasi/p/2880999.html
Copyright © 2011-2022 走看看