PHP头条
热点:

Twig 的 tags学习(中文) 之二


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

 

 

{% set foo = 'foo' %} 
 
{% set foo = [1, 2] %} 
 
{% set foo = {'foo': 'bar'} %} 
 
{% set foo = 'foo' ~ 'bar' %} 
 
{% 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还有一种用法,就是把 块内的内容赋值给变量


{% set foo %} 
  <div id="pagination"> 
    ... 
  </div> 
{% endset %} 
{% set foo %}
  <div id="pagination">
    ...
  </div>
{% endset %}


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

我们先来定义一个“基模板” base.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> 
<!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的作用就是告诉模板引擎,这里面的内容可以被子模板覆盖。

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


{% 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" %}

{% 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函数


<title>{% block title %}{% endblock %}</title> 
<h1>{{ block('title') }}</h1> 
{% block body %}{% endblock %} 
<title>{% block title %}{% endblock %}</title>
<h1>{{ block('title') }}</h1>
{% block body %}{% endblock %}

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


{% block sidebar %} 
    <h3>Table Of Contents</h3> 
    ... 
    {{ parent() }} 
{% endblock %} 
{% block sidebar %}
    <h3>Table Of Contents</h3>
    ...
    {{ parent() }}
{% endblock %}


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

{% block sidebar %} 
    {% block inner_sidebar %} 
        ... 
    {% endblock inner_sidebar %} 
{% endblock sidebar %} 
{% block sidebar %}
    {% block inner_sidebar %}
        ...
    {% endblock inner_sidebar %}
{% endblock sidebar %}

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


{% for item in seq %} 
    <li>{% block loop_item %}{{ item }}{% endblock %}</li> 
{% endfor %} 
{% for item in seq %}
    <li>{% block loop_item %}{{ item }}{% endblock %}</li>
{% endfor %}


简写block
以下这两种写法是等效的


{% block title %} 
    {{ page_title|title }} 
{% endblock %} 
 
{% block title page_title|title %} 
{% block title %}
    {{ page_title|title }}
{% endblock %}

{% block title page_title|title %}

 


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


{% extends some_var %} 
{% extends some_var %}
如果变量是一个twig模板对象,也可以。


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

$twig->display('template.twig', array('layout' => $layout));
1.2版本更新 你可以传递一个数组,twig会选择第一个存在的模板,来继承。


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


条件继承
这个很简单自己看吧,


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

 

block标签
参见 extends标签

 

 

include标签
载入一个模板,返回渲染的内容。载入的模板可以使用当前模板的变量{% include 'header.html' %} 
    Body 
{% include 'footer.html' %} 
{% include 'header.html' %}
    Body
{% include 'footer.html' %}

你可以给模板添加变量


{# 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 %} 
{# 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的变量{# only the foo variable will be accessible #} 
{% include 'foo' with {'foo': 'bar'} only %} 
 
{# no variable will be accessible #} 
{% 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表达式


{% include some_var %} 
{% include ajax ? 'ajax.html' : 'not_ajax.html' %} 
{% include some_var %}
{% include ajax ? 'ajax.html' : 'not_ajax.html' %}
也可以用twig模板对象


$template = $twig->loadTemplate('some_template.twig'); 
 
$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 关键字,这样当模板不存在的时候就不会引发错误。

 

 

{% include "sidebar.html" ignore missing %} 
{% include "sidebar.html" ignore missing with {'foo': 'bar} %} 
{% 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传递一个数组,他会自动载入第一个存在的模板{% include ['page_detailed.html', 'page.html'] %} 
{% include ['page_detailed.html', 'page.html'] %}

import 标签


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

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

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


{% 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 %} 
{% 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变量里)


{% 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> 
{% 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)

 

{% 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> 
{% 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


{# 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> 
{# 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到一个变量里,尽管这看起来很。。。没用。。


{# 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> 
{# 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标签

摘自 jiaochangyun的专栏
 

www.phpzy.comtrue/phprm/19072.htmlTechArticleTwig 的 tags学习(中文) 之二 set标签 主要是用来给变量赋值的。 {% set foo = foo %} {% set foo = [1, 2] %} {% set foo = {foo: bar} %} {% set foo = foo ~ bar %} {% set foo, bar = foo, bar %} {% set foo = foo %} {% set f...

相关文章

    暂无相关文章

PHP之友评论

今天推荐