Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

42 lines (24 sloc) 2.12 kb

Swig in Your Browser #

To compile Swig for rendering in the browser, clone the Swig repository and run the following in your command-line:

make && make browser

In the dist/browser/ directory, you will see 4 files:

Swig relies heavily on underscore.js. Because of this, there are two versions of Swig built for the browser. In dist/browser/, you will see 4 files:

  1. swig.js and swig.min.js
  2. swig.pack.js and swig.pack.min.js

You should only need one of these. If you already include underscore.js in your website, choose swig.js for development and swig.min.js for production. If you have not included underscore.js and you don't intend to, choose swig.pack.js for development and swig.pack.min.js for production.

The API #

Swig's API in the browser is the same as its API for node.js, with the only difference being that you cannot use the swig.compileFile method, since browsers do not have a filesystem.

Instead, you must always use swig.fromString and pre-parse all templates.

In order to use extends, import, and include correctly, another argument is available on swig.fromString: templateKey

swig.compile(templateString, { filename: templateKey });

This is the key that will be used to lookup a template. For instance:

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' });

When you render mypage, mypage.render({});, you will see

<p>Oh hey there!</p>

Known Issues #

  • Opera and Internet Explorer fail to comply with the date filter format r.
  • Internet Explorer 9.0 in compatibility mode and IE 6 through 8:
    • Is unable to compute the ISO date time for date filter formate c.
    • Does not handle unicode properly and so it fails to escape output for 'js'.
Jump to Line
Something went wrong with that request. Please try again.