You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Jazz is a simple template engine built specifically for nodejs.
Usage
var jazz = require("jazz");
var sys = require("sys");
var template = jazz.compile("my template source code {someVariable}");
template.eval({"someVariable": "lolmuffin"}, function(data) { sys.puts(data); });
This example would output the following:
my template source code lolmuffin
Syntax
Printing variables
{someVariable}
This works for any type of expression, so the following should also work:
{users.fred}
{"hello"}
{45}
{a eq b}
Filter functions
You can call filter functions like so:
{someFilter(arg1, arg2)}
Filter functions are statements, NOT expressions so they cannot be chained
nor used in if/forelse/etc. tests. However, calls can be made on any type
of expression -- e.g.
{math.sin(45)}
Implementing filter functions
Filter functions may block so rather than returning the value you want
rendered as you might in other frameworks, jazz passes in a callback to
your filter function that you then call to indicate that you have a
result. e.g. here we simulate a blocking operation using setTimeout().
{if user.lastName and user.isVip}
Hello, Mr. {user.lastName}, my good man!
{end}
{if fred.tired or fred.bored}
Fred: "Yawn!"
{end}
{if not awake}
Zzz
{end}
eq & neq comparison operators are available for comparing two values:
{if config.feature eq "enabled"}
Feature is enabled!
{end}
{if status neq "inactive"}
Huzzah!
{end}
You can also group expressions using parentheses:
{if (a and b) or c}
...
{end}
Looping over an array
{foreach item in someArray}
<p>{item}</p>
{end}
The value being iterated over can be any expression supporting
an Array-like interface.
Looping over an object
{foreach pair in someObject}
<p>{pair.key} = {pair.value}</p>
{end}
Synchronous functions
{if @blah('a')}
<p>There were so many blahs in a</p>
{end}
The function is provided to the template the same way asynchronous functions are, just with a return instead of a cb.
Loop counters / index
{foreach pair in someObject}
<p>Loop number (1 based): {__count}</p>
<p>Index (0 based): {__index}</p>
<p>{pair.key} = {pair.value}</p>
{end}