combine: node implementation
A template engine that hopefully works the way you'd expect :3
.
Getting started
To install combine
, you can clone this repository to your node_modules
folder or use the fantastic NPM
:
npm install node-combine
Then simply require
it in your projects to start using
var combine = ;
Basic usage
var tmpl = ;tmpl; // LOL
Features
TBD
Documentation
combine
works by parsing your template into a stack and rendering data.
Replacing template variables####
var template = '{{lol}}';var context = lol: 'test' ;var tmpl = ;var output = tmpl;/// output == 'test'
Using filters on variables####
var template = '{{test|reverse}}';var context = lol: 'test' ;var tmpl = ;tmplfilters;var output = tmpl;/// output == 'tset'
Conditionals####
Instead of being logic-less, combine
doesn't make any assumptions and
allows you to do things like if/elsif/else
with simple conditionals,
such as if something = somethingElse
or if not something
. You may
only test against strings, all data types will be coerced with toString().
var template = '{%if not test%}why not?{%endeach%}';var context = test: false ;var tmpl = ;var output = tmpl;/// output == 'why not?'
or a more complicated example...
var template = '{%if test = hello%}goodbye!{%else%}hello!{%endeach%}';var context = test: 'hello' ;var tmpl = ;var output = tmpl;/// output == 'goodbye!'
Iterating arrays####
Will not work on array-like objects, such as arguments or NodeList, coerce with
Array.prototype.slice.call(obj);
var template = '{%each test%}{{.}} {%endeach%}';var context = lol: 1234 ;var tmpl = ;var output = tmpl;/// output == '1 2 3 4 '
Iterating objects####
var template = '{%each test key,val%}the {{key}} is {{val}}{%endeach%}';var context =lol:hello: 'lol';var tmpl = ;var output = tmpl;/// output == 'the hello is lol'
Running unit tests
Run the follow command to fetch the Node.js
dependencies.
npm install
Then run the following command
make test