Search code examples
node.jsswig-template

Rendering a file with swig templates without using express


I use swig (django style templating engine) with express successfully all the time. I'm trying to use swig outside of request/render and simply pass context to a swig marked up file and have it generate the HTML.

I can't figure out the API without using express.

From the website they have this example that I can't get to work: https://github.com/paularmstrong/swig/tree/bd3f5a0dc08078ffd4372fa61248f8d50d8cbbf8

var template  = require('swig'); // v1.1.0
var tmpl = template.compileFile('/path/to/template.html');
tmpl.render({
    pagename: 'awesome people',
    authors: ['Paul', 'Jim', 'Jane']
});

There is an error saying render doesn't exist on the tmpl object? (The compileFile seems to be working fine.)

There is clearly something I'm not understanding or that I need to include outside of the example.

update: Added in version number of swig after comments to make it clear what version I'm working with.


Solution

  • From comments on original question:

    It looks like you're trying to use Swig@~1.0, yet the link you gave points to [email protected]. The API for Swig changed dramatically between v0 and v1.

    Documentation for the current supported version can always be found at http://paularmstrong.github.io/swig/