Skip to content

Instantly share code, notes, and snippets.

@rhengles rhengles/README.md
Created Jun 7, 2016

Embed
What would you like to do?
Nunjucks - minimal precompiled templates for Node

Usage:

npm install nunjucks nunjucks-cjs

node precompile.js

node render.js

var fs = require('fs');
var nunjucks = require('nunjucks');
var nunjucks_cjs = require('nunjucks-cjs');
var compiled = nunjucks.precompile('./', {
include: ['\\.html$'],
exclude: ['node_modules'],
wrapper: nunjucks_cjs.wrapper
});
fs.writeFile('templates-cjs.js', compiled, function(err) {
if (err) throw err;
console.log('file saved');
});
var fs = require('fs');
var nunjucks = require('nunjucks');
var templates = require('./templates-cjs.js');
var loader = new nunjucks.PrecompiledLoader(templates);
var env = new nunjucks.Environment([loader]);
var output = env.render('templates-slash-index.html', {
SOME_VAR: 'CONTEXT VALUE',
SOME_FUNCTION: function() {
return 'CONTEXT FUNCTION';
}
});
console.log(output);
Template file
Value: {{ SOME_VAR }}
Function: {{ SOME_FUNCTION() }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.