Swig

一个适用于Node.js和浏览器的模板引擎

命令行接口

安装

通过NPM安装:

$ npm install swig --global

用法

$ swig compile [file...] [options]
$ swig render [file...] [options]
$ swig run [file...] [options]

选项

-v, --version  Show the Swig version number.
-o, --output   Output location. [default: "stdout"]
-h, --help     Show this help screen.
-j, --json     Variable context as a JSON file.
-c, --context  Variable context as a CommonJS-style file. Used only if option `j` is not provided.
-m, --minify   Minify compiled functions with uglify-js
--filters      Custom filters as a CommonJS-style file
--tags         Custom tags as a CommonJS-style file
--options      Customize Swig's Options from a CommonJS-style file
--wrap-start   Template wrapper beginning for "compile". [default: "var tpl = "]
--wrap-end     Template wrapper end for "compile". [default: ";"]
--method-name  Method name to set template to and run from. [default: "tpl"]

例子

渲染一个文件:

$ swig render ./index.html

编译并将一个模板存储为一个可渲染的函数,且使用uglify-js来压缩输出:

$ swig compile ./index.html -m > ./cache/index.js

运行、编译已经缓存的模板:

$ swig run ./cache/index.js

将一个JSON文件作为变量环境导入来渲染模板:

$ swig render ./index.html -j ./index.json

将你的模板生成为一个AMD模块:

$ swig compile ./index.html --wrap-start="define(function () { return " --wrap-end="; });"