Swig

A Node.js and Browser-based JavaScript Template Engine

Command-Line Interface

Installation

Via NPM:

$ npm install swig --global

Usage

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

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"]

Examples

Render a single file:

$ swig render ./index.html

Compile and cache a template into a renderable function and minify the output with uglify-js:

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

Run and build the previously cached template:

$ swig run ./cache/index.js

Import a JSON file as the variable context for rendering a template:

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

Make your template an AMD module:

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