Simple templating for node.js based on John Resig's JavaScript Micro-Templating and Chad Etzel's template.node.js.
Also supports Express and any framework that follows the same view engine implementation.
npm install git+https://github.com/graphnode/node-template.git
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>
<html>
<body>
<% for (var i = 0; i < arr.length; i++) {
print('The value of arr ', i, ' is ', arr[i], ' <br/>');
} %>
</body>
</html>
template.create(str, data, callback)
Parameters:
str - html or filename of template to load
data - object containing data to replace in the template
callback - optional argument for async coding
Returns:
String of the template file with all code/variables replaced with data object contents
Example:
var foo = template.tmpl("./hello.template", {name:"Chad"});
console.log(foo);
template.create(str, callback)
Parameters:
str - html or filename of template to load
callback - optional argument for async coding
Returns:
Pre-compiled/generated function to which you can pass a data object
Example:
var bar = template.tmpl("./hello.template");
var baz = bar({name:"Bob"});
console.log(baz);
Templates are cached in the "cache" property of the module. Change the "useCache" property to false if you don't want to use the cache. They are cached with the template as the key, please avoid caching huge templates and instead pre-compile them.