引入库app.jshtml
var render = require('koa-swig');
模版设置app.jsjson
app.context.render = co.wrap(render({ root: __dirname + '/views', autoescape: true, cache: 'memory', // disable, set to false ext: 'html' }));
在another.js调用api
router.get('/', function *(next) { console.log(this); yield this.render('index', { title: 'Hello World' }); });
这样就能够显示views/index.html了,后面的title设置的参数 数组
index.html页面代码app
{% extends 'layout.html' %}
{% block title %} {{title}} {% endblock%}
{% block cotent %} <p> 测试</p> {% endblock %}
layout.html页面代码less
<!doctype html> <html> <head> <meta charset="utf-8"> <meta content="中文"> <title>{% block title %}My Site{% endblock %}</title> </head> <body> {% block content %}{% endblock %} </body> </html>
index.html页面中的 block title 的内容会填入layout.html页面的block title中koa
{% include "login.html" %}
你能够标记 ignore missing,这样若是模板不存在,也不会抛出错误函数
{% include "foobar.html" ignore missing %}
若是想把本地声明的变量引入到包含的模板种,可使用 with 参数来把后面的对象建立到包含模板的上下文中oop
{% set foo = { bar: "baz" } %} {% include "inc.html" with foo %} {% for bar in thing %} {% include "inc.html" with bar %} {% endfor %}
1.demo测试
{{ name|title }} was born on {{ birthday|date('F jS, Y') }} and has {{ bikes|length|default("zero") }} bikes.
也可使用 filter 标签来为块内容添加过滤器
{% filter upper %}oh hi, paul{% endfilter %}
建立一个 myfilter.js 而后引入到 Swig 的初始化函数中
swig.init({ filters: require('myfilters') });
在 myfilter.js 里,每个 filter 方法都是一个简单的 js 方法,下例是一个翻转字符串的 filter:
exports.myfilter = function (input) { return input.toString().split('').reverse().join(''); };
你的 filter 一旦被引入,你就能够向下面同样使用:
{{ name|myfilter }} {% filter myfilter %} I shall be filtered {% endfilter %}
{% for x in y %} {% if loop.first %}<ul>{% endif %} <li>{{ loop.index }} - {{ loop.key }}: {{ x }}</li> {% if loop.last %}</ul>{% endif %} {% endfor %}
loop.index:当前循环的索引(1开始) loop.index0:当前循环的索引(0开始) loop.revindex:当前循环从结尾开始的索引(1开始) loop.revindex0:当前循环从结尾开始的索引(0开始) loop.key:若是迭代是对象,是当前循环的键,不然同 loop.index loop.first:若是是第一个值返回 true loop.last:若是是最后一个值返回 true loop.cycle:一个帮助函数,以指定的参数做为周期 ```
在 for 标签里使用 else
{% for person in people %} {{ person }} {% else %} There are no people yet! {% endfor %}
if:条件语句
{% if x %}{% endif %} {% if !x %}{% endif %} {% if not x %}{% endif %} {% if x and y %}{% endif %} {% if x && y %}{% endif %} {% if x or y %}{% endif %} {% if x || y %}{% endif %} {% if x || (y && z) %}{% endif %} {% if x [operator] y %} Operators: ==, !=, <, <=, >, >=, ===, !== {% endif %} {% if x == 'five' %} The operands can be also be string or number literals {% endif %} {% if x|length === 3 %} You can use filters on any operand in the statement. {% endif %} {% if x in y %} If x is a value that is present in y, this will return true. {% endif %}
else 和 else if
{% if foo %} Some content. {% else if "foo" in bar %} Content if the array `bar` has "foo" in it. {% else %} Fallback content. {% endif %}
autoescape:改变当前变量的自动转义行为
假设
some_html_output = '<p>Hello "you" & \'them\'</p>';
而后 {% autoescape false %} {{ some_html_output }} {% endautoescape %} {% autoescape true %} {{ some_html_output }} {% endautoescape %} {% autoescape true "js" %} {{ some_html_output }} {% endautoescape %}
set:设置一个变量,在当前上下文中复用
{% set foo = [0, 1, 2, 3, 4, 5] %} {% for num in foo %} <li>{{ num }}</li> {% endfor %}
macro:建立自定义可服用的代码段
{% macro input type name id label value error %} <label for="{{ name }}">{{ label }}</label> <input type="{{ type }}" name="{{ name }}" id="{{ id }}" value="{{ value }}"{% if error %} class="error"{% endif %}> {% endmacro %}
而后像下面使用
<div> {{ input("text", "fname", "fname", "First Name", fname.value, fname.errors) }} </div> <div> {{ input("text", "lname", "lname", "Last Name", lname.value, lname.errors) }} </div>
输出以下
<div> <label for="fname">First Name</label> <input type="text" name="fname" id="fname" value="Paul"> </div> <div> <label for="lname">Last Name</label> <input type="text" name="lname" id="lname" value="" class="error"> </div>
import:容许引入另外一个模板的宏进入当前上下文
{% import 'formmacros.html' as form %} {# this will run the input macro #} {{ form.input("text", "name") }} {# this, however, will NOT output anything because the macro is scoped to the "form" object: #} {{ input("text", "name") }}
spaceless:尝试移除html标签间的空格
{% spaceless %} {% for num in foo %} <li>{{ loop.index }}</li> {% endfor %} {% endspaceless %}
参考:http://www.iqianduan.net/blog/how_to_use_swig