Created
June 26, 2011 01:47
-
-
Save honza/1047130 to your computer and use it in GitHub Desktop.
Django-style Mustache-power templates for node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
</head> | |
<body> | |
{{>main }} | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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