node-combine

0.0.1 • Public • Published

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 = require('node-combine');

Basic usage

var tmpl = combine('{{test}}', { test: 'lol' });
tmpl.render(); // 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 = combine(template, context);
 
var output = tmpl.render();
/// output == 'test'

Using filters on variables####

var template = '{{test|reverse}}';
var context = { lol: 'test' };
 
var tmpl = combine(template, context);
tmpl.filters.add('reverse', function(val) {
  return val.split('').reverse().join('');
});
 
var output = tmpl.render();
/// 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 = combine(template, context);
 
var output = tmpl.render();
/// output == 'why not?'

or a more complicated example...

var template = '{%if test = hello%}goodbye!{%else%}hello!{%endeach%}';
var context = { test: 'hello' };
 
var tmpl = combine(template, context);
 
var output = tmpl.render();
/// 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: [1,2,3,4] };
 
var tmpl = combine(template, context);
 
var output = tmpl.render();
/// 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 = combine(template, context);
 
var output = tmpl.render();
/// 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

Readme

Keywords

none

Package Sidebar

Install

npm i node-combine

Weekly Downloads

1

Version

0.0.1

License

none

Last publish

Collaborators

  • tbranyen