A templating system for node.js based on John Resig's JavaScript Micro-Templating.
Unlike that version (and Chad Etzel's) this one preserves whitespace allowing you to use blocks of (and inline) javascript and css in the page. It also has no problem with single quotes.
Templates are just files with special <% %> tags (like PHP or Ruby tags) which will be replaced with passed-in data. Templates can also contain javascript code to be expanded.
<html>
<body>
Hello, <%=name>.
</body>
</html>
<html>
<body>
<% for (var i = 0; i < arr.length; i++) { %>
The value of arr[<%=i%>] is <%=arr[i]%> <br/>
<% } %>
</body>
</html>
There are plenty of ways to use this:
sys.puts(template.create("Hello <%=word%>!", {word:"world"}));
var t = template.create("Hello <%=word%>!");
sys.puts(t({word:"planet"}));
template.create("Hello <%=word%>!", {word:kitty"}, function(t) {
sys.puts(t);
});
- Callbacks with non-processed templates.
- Cache by filename (not string).
- Auto-cache, reloads the cached file if a change is detected in the file. (This can be turned off on production.)
You do not need to restart the node app to see the changes take effect. The reason is because it caches strings and not files. This has the downside of filling memory with useless templates until you restart the app. This should be fixed in a future version together with auto-cache.