标签:正式 cti option 没有 charset 匹配 提前 NPU number
swig.init({ allowErrors: false, autoescape: true, cache: true, encoding: ‘utf8‘, filters: {}, root: ‘/‘, tags: {}, extensions: {}, tzOffset: 0 });
options:
var tpl = swig.compileFile("path/to/template/file.html"); var renderedHtml = tpl.render({ vars: ‘to be inserted in template‘ });
或者
var tpl = swig.compile("Template string here"); var renderedHtml = tpl({ vars: ‘to be inserted in template‘ });
npm install express
npm install consolidate
然后
app.engine(‘.html‘, cons.swig);
app.set(‘view engine‘, ‘html‘);
Swig浏览器版本的api基本与nodejs版相同,不同点如下:
var template = swig.compile(‘<p>{% block content %}{% endblock %}</p>‘, { filename: ‘main‘ }); var mypage = swig.compile(‘{% extends "main" %}{% block content %}Oh hey there!{% endblock %}‘, { filename: ‘mypage‘ });
{{ foo.bar }}
{{ foo[‘bar‘] }}
如果变量未定义,输出空字符。
变量可以通过过滤器来修改:
{{ name|title }} was born on {{ birthday|date(‘F jS, Y‘) }}
// Jane was born on July 6th, 1985
参见标签部分。
模板里的空白在最终输出时默认保留,如果需要去掉空白,可以在逻辑标签前后加上空白控制服-:
{% for item in seq -%}
{{ item }}
{%- endfor %}
Swig 使用 extends 和 block 来实现模板继承 layout.html
<!doctype html> <html> <head> <meta charset="utf-8"> <title>{% block title %}My Site{% endblock %}</title> {% block head %} <link rel="stylesheet" href="main.css"> {% endblock %} </head> <body> {% block content %}{% endblock %} </body> </html>
index.html
{% extends ‘layout.html‘ %}
{% block title %}My Page{% endblock %}
{% block head %}
{% parent %}
<link rel="stylesheet" href="custom.css">
{% endblock %}
{% block content %}
<p>This is just an awesome page.</p>
{% endblock %}
用于修改变量。变量名称后用 | 字符分隔添加过滤器。您可以添加多个过滤器。
{{ 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 %}
你也可以像下面一样给 filter 传参数:
exports.prefix = function(input, prefix) { return prefix.toString() + input.toString(); }; {{ name|prefix(‘my prefix‘) }} {% filter prefix ‘my prefix‘ %I will be prefixed with "my prefix".{% endfilter %} {% filter prefix foo %}I will be prefixed with the value stored to `foo`.{% endfilter %}
extends:使当前模板继承父模板,必须在文件最前
{% include template_path %}
{% include "path/to/template.js" %}
你可以标记 ignore missing,这样如果模板不存在,也不会抛出错误
{% include "foobar.html" ignore missing %}
本地声明的上下文变量,默认情况不会传递给包含的模板。例如以下情况,inc.html 无法得到 foo 和 bar
{% set foo = "bar" %}
{% include "inc.html" %}
{% for bar in thing %}
{% include "inc.html" %}
{% endfor %}
如果想把本地声明的变量引入到包含的模板种,可以使用 with 参数来把后面的对象创建到包含模板的上下文中
{% set foo = { bar: "baz" } %}
{% include "inc.html" with foo %}
{% for bar in thing %}
{% include "inc.html" with bar %}
{% endfor %}
如果当前上下文中 foo 和 bar 可用,下面的情况中,只有 foo 会被 inc.html 定义
{% include "inc.html" with foo only %}
only 必须作为最后一个参数,放在其他位置会被忽略
raw:停止解析标记中任何内容,所有内容都将输出
{% 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 item in items %}
<li class="{{ loop.cycle(‘odd‘, ‘even‘) }}">{{ item }}</li>
{% endfor %}
在 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 %}
将会输出
<p>Hello "you" & ‘them‘</p> <p>Hello "you" & 'them' </p> \u003Cp\u003EHello \u0022you\u0022 & \u0027them\u0027\u003C\u005Cp\u003E
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") }}
filter:对整个块应用过滤器
{% filter uppercase %}
oh hi, {{ name }}
{% endfilter %}
{% filter replace "." "!" "g" %}
Hi. My name is Paul.
{% endfilter %}
输出
OH HI, PAUL Hi! My name is Paul!
spaceless:尝试移除html标签间的空格
{% spaceless %}
{% for num in foo %}
<li>{{ loop.index }}</li>
{% endfor %}
{% endspaceless %}
输出
<li>1</li><li>2</li><li>3</li>
标签:正式 cti option 没有 charset 匹配 提前 NPU number
原文地址:https://www.cnblogs.com/wangking/p/9857259.html