Skip to content

Instantly share code, notes, and snippets.

@honza
Created June 26, 2011 01:47
Show Gist options
  • Save honza/1047130 to your computer and use it in GitHub Desktop.
Save honza/1047130 to your computer and use it in GitHub Desktop.
Django-style Mustache-power templates for node.js
var fs = require('fs');
var mustache = require('mustache');
var BASE = fs.readFileSync('base.html', 'utf-8');
var CACHE = {};
var context = {
name: 'Honza',
age: 22
};
var render = function(template_name, context) {
if (!CACHE[template_name]) {
CACHE[template_name] = fs.readFileSync(template_name, 'utf-8');
}
var partials = {
main: CACHE[template_name]
};
return mustache.to_html(BASE, context, partials);
};
var html = render('index.html', context);
console.log(html);
<html>
<head>
</head>
<body>
{{>main }}
</body>
</html>
<div id="wrap">
<h1>{{ name }}</h1>
<h3>{{ age }}</h3>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment