Skip to content

graphnode/node-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

7 Commits

Repository files navigation

node-template

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.

Installing

npm install git+https://github.com/graphnode/node-template.git 

Templates

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.

Example Template

<html> <body> Hello, <%=name>. </body> </html> 

Example Template with javascript

<html> <body> <% for (var i = 0; i < arr.length; i++){%> The value of arr[<%=i%>] is <%=arr[i]%> <br/> <% } %> </body> </html> 

Using the print function instead

<html> <body> <% for (var i = 0; i < arr.length; i++){print('The value of arr ', i, ' is ', arr[i], ' <br/>')} %> </body> </html> 

Usage

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); 

Other Info

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.

About

Simple templating for node.js

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published