必要条件
Twig需要的最低PHP版本为5.2.4。
安装Subversion或者Git
SVN地址:http://svn.twig-project.org/trunk/, git地址git://github.com/fabpot/Twig.git
使用Twig的第一步是注册它的autoloader:
2
|
require_once
'/path/to/lib/Twig/Autoloader.php'
;
Twig_Autoloader::register();
|
记得用Twig所在路径代替/path/to/lib
2
|
require_once
'/path/to/lib/Twig/Autoloader.php'
;
Twig_Autoloader::register();
|
注:Twig在类的命名上遵守PEAR的约定,这意味着你可以在自己编写的autoloader中整合对Twig的类的加载。
有关TWIG 模板引擎 快速入门手册需要原文、英文的朋友们,可以在这里找到 http://twig.sensiolabs.org/doc/templates.html
The flexible, fast, and secure
template engine for PHP
Twig for Template Designers( Twig摸板设计者)
以下文章描述的是Twig魔板引擎和将要更好的使用创建Twig模板而生。
twig 的模板就是普通的文本文件,也不需要特别的扩展名,.html .htm .twig 都可以。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!DOCTYPE html>
<html>
<head>
<title>My Webpage</title>
</head>
<body>
<ul id="navigation">
{% for item in navigation %}
<li><a href="{{ item.href }}">{{ item.caption }}</a></li>
{% endfor %}
</ul>
<h1>My Webpage</h1>
{{ a_variable }}
</body>
</html>
|
IDEs Integration¶
- Textmate via the Twig bundle
- Vim via the Jinja syntax plugin or the vim-twig plugin
- Netbeans via the Twig syntax plugin (until 7.1, native as of 7.2)
- PhpStorm (native as of 2.1)
- Eclipse via the Twig plugin
- Sublime Text via the Twig bundle
- GtkSourceView via the Twig language definition (used by gedit and other projects)
- Coda and SubEthaEdit via the Twig syntax mode
- Coda 2 via the other Twig syntax mode
- Komodo and Komodo Edit via the Twig highlight/syntax check mode
- Notepad++ via the Notepad++ Twig Highlighter
- Emacs via web-mode.el
- Atom via the PHP-twig for atom
Also, TwigFiddle is an online service that allows you to execute Twig templates from a browser; it supports all versions of Twig.
Variables(变量)
1 2 |
{{ foo.bar }}
{{ foo['bar'] }}
|
全局变量
TWIG定义了有一些全局变量
- _self 这个参看macro标签
- _context 这个就是当前的环境
- _charset: 当前的字符编码
1 2 3 |
{% set foo = 'foo' %}
{% set foo = [1, 2] %}
{% set foo = {'foo': 'bar'} %}
|
过滤器 Firters
1 |
{{ name|striptags|title }}
|
Striptags表示去除html标签,title表示每个单词的首字母大写。
1 |
{{ list|join(', ') }}
|
To apply a filter on a section of code, wrap it in the filter tag:
1 2 3 |
{% filter upper %}
This text becomes uppercase
{% endfilter %}
|
函数 Function
{% for i in range(0, 3) %} {{ i }}, {% endfor %}
命名结构
{% for i in range(low=1, high=10, step=2) %} {{ i }}, {% endfor %}
2 3 4 5 |
{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}
{# versus #}
{{ data|convert_encoding(from='iso-2022-jp', to='UTF-8') }}
|
流程控制
2 3 4 5 |
{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}
{# versus #}
{{ data|convert_encoding(from='iso-2022-jp', to='UTF-8') }}
|
<h1>Members</h1>
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
|
The if tag can be used to test an expression:
1 2 3 4 5 6 7 |
{% if users|length > 0 %}
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
{% endif %}
|
2 3 4 5 |
{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}
{# versus #}
{{ data|convert_encoding(from='iso-2022-jp', to='UTF-8') }}
|
注释
{# ... #} 包围的内容会被注释掉,可以是单行 也可以是多行。
2 3 4 5 |
{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}
{# versus #}
{{ data|convert_encoding(from='iso-2022-jp', to='UTF-8') }}
|
{# note: disabled template because we no longer use this {% for user in users %} ... {% endfor %} #}
载入其他模板
1 |
{{ include('sidebar.html') }}
|
1 2 3 |
{% for box in boxes %}
{{ include('render_box.html') }}
{% endfor %}
|
1 |
{{ include('sections/articles/sidebar.html') }}
|
This behavior depends on the application embedding Twig.
2 3 4 5 |
{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}
{# versus #}
{{ data|convert_encoding(from='iso-2022-jp', to='UTF-8') }}
|
模板继承
TWIG中最有用到功能就是模板继承,他允许你建立一个“骨骼模板”,然后你用不同到block来覆盖父模板中任意到部分。而且使用起来非常到简单。
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!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>
|
{% 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 to my awesome homepage. </p> {% endblock %}
|
|
HTML转义
因为{{是TWIG的操作符,如果你需要输出两个花括号,最简单到办法就是
还可以使用 raw 标签和raw 过滤器,详细参考手册
macros宏
表达式
你还可以在一个字符串内部插入一个表达式,通常这个表达式是变量。 格式是 #{表达式}
空白控制
和 php一样,在TWIG模板标签之后的第一个换行符会被自动删掉,其余的空白(包括 空格 tab 换行等)都会被原样输出。使用-操作符,可以很方便的删除TWIG标签之前或之后与html标签之间的空白。
2 3 4 5 |
{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}
{# versus #}
{{ data|convert_encoding(from='iso-2022-jp', to='UTF-8') }}
|
2
|
require_once
'/path/to/lib/Twig/Autoloader.php'
;
Twig_Autoloader::register();
|
{% if users %}
<ul>
{% for user in users %}
<li>{{ user.username }}</li>
{% endfor %}
</ul>
{% endif %}