Twig 的 tags学习(中文) 之二

 

set标签

主要是用来给变量赋值的。

 

 

  1. {% set foo = 'foo' %} 
  2.  
  3. {% set foo = [1, 2] %} 
  4.  
  5. {% set foo = {'foo': 'bar'} %} 
  6.  
  7. {% set foo = 'foo' ~ 'bar' %} 
  8.  
  9. {% set foo, bar = 'foo', 'bar' %} 
{% set foo = 'foo' %}

{% set foo = [1, 2] %}

{% set foo = {'foo': 'bar'} %}

{% set foo = 'foo' ~ 'bar' %}

{% set foo, bar = 'foo', 'bar' %}

其中 'foo'~'bar' 这个我没怎么看明白,测试了一下,可能是字符串连接的。

 

set还有一种用法,就是把 块内的内容赋值给变量

 

  1. {% set foo %} 
  2.   <divid="pagination"> 
  3.     ... 
  4.   </div> 
  5. {% endset %} 
{% set foo %}
  <div id="pagination">
    ...
  </div>
{% endset %}

 

extends标签

这个标签用来表示本模板继承自另外一个模板。和php一样,twig不支持多重继承,所以你只能有一个extends标签,而且要在模板的最上方。

我们先来定义一个“基模板” base.html 他就像一个骨架一个。

 

  1. <!DOCTYPE html> 
  2. <html> 
  3.     <head> 
  4.         {% block head %} 
  5.             <linkrel="stylesheet"href="style.css"/> 
  6.             <title>{% block title %}{% endblock %} - My Webpage</title> 
  7.         {% endblock %} 
  8.     </head> 
  9.     <body> 
  10.         <divid="content">{% block content %}{% endblock %}</div> 
  11.         <divid="footer"> 
  12.             {% block footer %} 
  13.                 © Copyright 2011 by <ahref="http://domain.invalid/">you</a>
  14.             {% endblock %} 
  15.         </div> 
  16.     </body> 
  17. </html> 
<!DOCTYPE html>
<html>
    <head>
        {% block head %}
            <link rel="stylesheet" href="style.css" />
            <title>{% block title %}{% endblock %} - My Webpage</title>
        {% endblock %}
    </head>
    <body>
        <div id="content">{% block content %}{% endblock %}</div>
        <div id="footer">
            {% block footer %}
                © Copyright 2011 by <a href="http://domain.invalid/">you</a>.
            {% endblock %}
        </div>
    </body>
</html>

{% block %}标签定义了4个区块(block head, block title, block content, block footer),可以让子模板来填充内容。block的作用就是告诉模板引擎,这里面的内容可以被子模板覆盖。

 

一个子模板大概类似于这样的

 

  1. {% extends "base.html" %} 
  2.  
  3. {% block title %}Index{% endblock %} 
  4. {% block head %} 
  5.     {{ parent() }} 
  6.     <styletype="text/css"> 
  7.         .important { color: #336699; } 
  8.     </style> 
  9. {% endblock %} 
  10. {% block content %} 
  11.     <h1>Index</h1> 
  12.     <pclass="important"> 
  13.         Welcome on my awesome homepage. 
  14.     </p> 
  15. {% endblock %} 
{% extends "base.html" %}

{% block title %}Index{% endblock %}
{% block head %}
    {{ parent() }}
    <style type="text/css">
        .important { color: #336699; }
    </style>
{% endblock %}
{% block content %}
    <h1>Index</h1>
    <p class="important">
        Welcome on my awesome homepage.
    </p>
{% endblock %}

extends是非常关键的,它告诉模板引擎,本模板继承自另一个模板(base.html)。当模板引擎解析到本模板时,会首先载入父模板。extends标签应该是模板内的第一个标签。

 

如果子模板没有定义block footer ,那么父模板会用默认值代替。

注意:block标签的名字是不能重复的。如果你想让同一个block多次打印。可以使用block函数

 

  1. <title>{% block title %}{% endblock %}</title> 
  2. <h1>{{ block('title') }}</h1> 
  3. {% block body %}{% endblock %} 
<title>{% block title %}{% endblock %}</title>
<h1>{{ block('title') }}</h1>
{% block body %}{% endblock %}

 

父block

也许你会需要 父block的内容。可以使用parent函数,这很有用比如你想往一个block里添加内容而不是覆盖时。

 

  1. {% block sidebar %} 
  2.     <h3>Table Of Contents</h3> 
  3.     ... 
  4.     {{ parent() }} 
  5. {% endblock %} 
{% block sidebar %}
    <h3>Table Of Contents</h3>
    ...
    {{ parent() }}
{% endblock %}

 

命名endblock

模板引擎 允许你命名结束标记,这样可读性会提高很多。但个人觉得没啥用处。

  1. {% block sidebar %} 
  2.     {% block inner_sidebar %} 
  3.         ... 
  4.     {% endblock inner_sidebar %} 
  5. {% endblock sidebar %} 
{% block sidebar %}
    {% block inner_sidebar %}
        ...
    {% endblock inner_sidebar %}
{% endblock sidebar %}

嵌套block

允许你嵌套生成block ,来形成更复杂的block

 

  1. {% for item in seq %} 
  2.     <li>{% block loop_item %}{{ item }}{% endblock %}</li> 
  3. {% endfor %} 
{% for item in seq %}
    <li>{% block loop_item %}{{ item }}{% endblock %}</li>
{% endfor %}

 

简写block

以下这两种写法是等效的

 

  1. {% block title %} 
  2.     {{ page_title|title }} 
  3. {% endblock %} 
  4.  
  5. {% block title page_title|title %} 
{% block title %}
    {{ page_title|title }}
{% endblock %}

{% block title page_title|title %}

 

动态继承

你可以用一个变量来继承不同的模板。

 

  1. {% extends some_var %} 
{% extends some_var %}

如果变量是一个twig模板对象,也可以。

 

 

  1. $layout = $twig->loadTemplate('some_layout_template.twig'); 
  2.  
  3. $twig->display('template.twig', array('layout' => $layout)); 
$layout = $twig->loadTemplate('some_layout_template.twig');

$twig->display('template.twig', array('layout' => $layout));

1.2版本更新 你可以传递一个数组,twig会选择第一个存在的模板,来继承。

 

 

  1. {% extends ['layout.html', 'base_layout.html'] %} 
{% extends ['layout.html', 'base_layout.html'] %}

 

条件继承

这个很简单自己看吧,

 

  1. {% extends standalone ? "minimum.html" : "base.html" %} 
{% extends standalone ? "minimum.html" : "base.html" %}

 

block标签

参见 extends标签

 

include标签

载入一个模板,返回渲染的内容。载入的模板可以使用当前模板的变量

  1. {% include 'header.html' %} 
  2.     Body 
  3. {% include 'footer.html' %} 
{% include 'header.html' %}
    Body
{% include 'footer.html' %}

你可以给模板添加变量

 

  1. {# the foo template will have access to the variables from the current context and the foo one #} 
  2. {% include 'foo' with {'foo': 'bar'} %} 
  3.  
  4. {% set vars = {'foo': 'bar'} %} 
  5. {% include 'foo' with vars %} 
{# the foo template will have access to the variables from the current context and the foo one #}
{% include 'foo' with {'foo': 'bar'} %}

{% set vars = {'foo': 'bar'} %}
{% include 'foo' with vars %}

你也可以使用 only 关键字 来禁止载入的模板使用当前模板的变量,只能使用include 时with的变量

  1. {# only the foo variable will be accessible #} 
  2. {% include 'foo' with {'foo': 'bar'} only %} 
  3.  
  4. {# no variable will be accessible #} 
  5. {% include 'foo' only %} 
{# only the foo variable will be accessible #}
{% include 'foo' with {'foo': 'bar'} only %}

{# no variable will be accessible #}
{% include 'foo' only %}

载入的模板名也可以是一个twig表达式

 

 

  1. {% include some_var %} 
  2. {% include ajax ? 'ajax.html' : 'not_ajax.html' %} 
{% include some_var %}
{% include ajax ? 'ajax.html' : 'not_ajax.html' %}

也可以用twig模板对象

 

 

  1. $template = $twig->loadTemplate('some_template.twig'); 
  2.  
  3. $twig->loadTemplate('template.twig')->display(array('template' => $template)); 
$template = $twig->loadTemplate('some_template.twig');

$twig->loadTemplate('template.twig')->display(array('template' => $template));

1.2版本新加内容,可以在模板加上 ignore missing 关键字,这样当模板不存在的时候就不会引发错误。

 

  1. {% include "sidebar.html" ignore missing %} 
  2. {% include "sidebar.html" ignore missing with {'foo': 'bar} %} 
  3. {% include "sidebar.html" ignore missing only %} 
{% include "sidebar.html" ignore missing %}
{% include "sidebar.html" ignore missing with {'foo': 'bar} %}
{% include "sidebar.html" ignore missing only %}

1.2版本新加内容,你可以给include传递一个数组,他会自动载入第一个存在的模板

  1. {% include ['page_detailed.html', 'page.html'] %} 
{% include ['page_detailed.html', 'page.html'] %}

import 标签

twig允许把一些常用的代码放入到macros(宏)里,这些macros被不同的模板导入。

有两种方法导入模板,你可以导入整个模板到一个变量里,或者只导入需要的几个macros

假如我们有个助手模块,来帮助我们渲染表单(forms.html)

 

  1. {% macro input(name, value, type, size) %} 
  2.     <inputtype="{{ type|default('text') }}"name="{{ name }}"value="{{ value|e }}"size="{{ size|default(20) }}"/> 
  3. {% endmacro %} 
  4.  
  5. {% macro textarea(name, value, rows) %} 
  6.     <textareaname="{{ name }}"rows="{{ rows|default(10) }}"cols="{{ cols|default(40) }}">{{ value|e }}</textarea> 
  7. {% endmacro %} 
{% macro input(name, value, type, size) %}
    <input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" />
{% endmacro %}

{% macro textarea(name, value, rows) %}
    <textarea name="{{ name }}" rows="{{ rows|default(10) }}" cols="{{ cols|default(40) }}">{{ value|e }}</textarea>
{% endmacro %}

最简单,最灵活的办法就是导入整个模板。(把模板导入到 forms变量里)

 

 

  1. {% import 'forms.html' as forms %} 
  2.  
  3. <dl> 
  4.     <dt>Username</dt> 
  5.     <dd>{{ forms.input('username') }}</dd> 
  6.     <dt>Password</dt> 
  7.     <dd>{{ forms.input('password', null, 'password') }}</dd> 
  8. </dl> 
  9. <p>{{ forms.textarea('comment') }}</p> 
{% import 'forms.html' as forms %}

<dl>
    <dt>Username</dt>
    <dd>{{ forms.input('username') }}</dd>
    <dt>Password</dt>
    <dd>{{ forms.input('password', null, 'password') }}</dd>
</dl>
<p>{{ forms.textarea('comment') }}</p>

或者你可以导入模板的名字到当前的名字空间下。 (导入input,textarea 并把input重名为input_field)

 

 

  1. {% from 'forms.html' import input as input_field, textarea %} 
  2.  
  3. <dl> 
  4.     <dt>Username</dt> 
  5.     <dd>{{ input_field('username') }}</dd> 
  6.     <dt>Password</dt> 
  7.     <dd>{{ input_field('password', '', 'password') }}</dd> 
  8. </dl> 
  9. <p>{{ textarea('comment') }}</p> 
{% from 'forms.html' import input as input_field, textarea %}

<dl>
    <dt>Username</dt>
    <dd>{{ input_field('username') }}</dd>
    <dt>Password</dt>
    <dd>{{ input_field('password', '', 'password') }}</dd>
</dl>
<p>{{ textarea('comment') }}</p>

如果是当前模板内定义的macros,那就不必导入了,直接使用特殊变量_self

 

 

  1. {# index.html template #} 
  2.  
  3. {% macro textarea(name, value, rows) %} 
  4.     <textareaname="{{ name }}"rows="{{ rows|default(10) }}"cols="{{ cols|default(40) }}">{{ value|e }}</textarea> 
  5. {% endmacro %} 
  6.  
  7. <p>{{ _self.textarea('comment') }}</p> 
{# index.html template #}

{% macro textarea(name, value, rows) %}
    <textarea name="{{ name }}" rows="{{ rows|default(10) }}" cols="{{ cols|default(40) }}">{{ value|e }}</textarea>
{% endmacro %}

<p>{{ _self.textarea('comment') }}</p>

那么你仍然可以导入_self到一个变量里,尽管这看起来很。。。没用。。

 

 

  1. {# index.html template #} 
  2.  
  3. {% macro textarea(name, value, rows) %} 
  4.     <textareaname="{{ name }}"rows="{{ rows|default(10) }}"cols="{{ cols|default(40) }}">{{ value|e }}</textarea> 
  5. {% endmacro %} 
  6.  
  7. {% import _self as forms %} 
  8.  
  9. <p>{{ forms.textarea('comment') }}</p> 
{# index.html template #}

{% macro textarea(name, value, rows) %}
    <textarea name="{{ name }}" rows="{{ rows|default(10) }}" cols="{{ cols|default(40) }}">{{ value|e }}</textarea>
{% endmacro %}

{% import _self as forms %}

<p>{{ forms.textarea('comment') }}</p>

from标签

参见 import标签

posted @ 2013-01-29 10:41  V.Wang  阅读(471)  评论(0编辑  收藏  举报