tarima

Multi-templating for NodeJS

$ npm install tarima --save-dev

Tarima is a pipeline library for templating based on the filename extensions.

Define a template named tpl.js.jade and explicitly call render() or compile():

var tarima = require('tarima');
 
var template = 'h1 It works!';
 
var view = tarima.parse('tpl.js.jade', template);
 
console.log(view.render()); // <h1>It works!</h1> 
console.log(view.compile()); // function template(locals) ... 

Now rename your template into tpl.html.jade and the output will be the same.

Tarima understands the .html.jade extension as: render jade into markup since it can't compile into html.

While .js.ract.jade means: render jade into markup, then compile ract into javascript.

You can use .css.less to render css, or .js.less to compile into a js function, etc.

Although not all filename extensions are chainable you can mix them on several and useful ways.

  • CoffeeScript can compile only into .js
  • CSS can be compiled into .js; render .ejs and .hbs
  • EJS can compile and render from anything (?)
  • Handlebars can compile and render from anything (?)
  • HTML can be compiled into .js; render .ejs, .hbs; render from .ract
  • Jade can compile into .js; render .ejs, .hbs and .html
  • Javascript anything can compile into this (?)
  • JSON can compile only into .js
  • LESS can compile into .js; render .ejs, .hbs and .css
  • Markdown can compile into .js; render .ejs, .hbs and .html; render from .ract; .coffee render as Literate CoffeeScript
  • Ractive can compile into .js; render .ejs, .hbs and .html

Register a custom extension and callback.

The callback receive the partial parameters and a next() callback for validating supported target extensions:

tarima.add('foo', function(paramsnext) {
  if (next('baz', 'bar')) {
    // ... 
  }
});
 
// tpl.bar.foo => OK 
// tpl.baz.foo => OK 
// tpl.buzz.foo => EROR 

If the target extension is not allowed an exception will be thrown.

The params object looks like:

{
  filepath: '',
  filename: 'foo.js.coffee',
  parts: [ 'coffee' ],
  name: 'foo',
  ext: 'js',
  options: { locals: {} },
  source: '-> "string"',
  keypath: 'foo',
  type: 'coffee',
  client: false
}

Using params.source the callback must build the string to render or compile it.

If params.client is true it must return a compiled function to render on the client:

var tpl = new TemplateEngine(params.source, params.options);
 
if (params.client) {
  return tpl.compileClient();
}
 
return tpl.compile();

In the browser is required to provide a runtime for some engines.

Generate a partial view from the given source and filepath.

var view = tarima.parse('foo.js.jade', 'h1 It works');

Same as parse but reading from filesystem instead.

var view = tarima.load('/path/to/foo.js.jade');

Please open a ticket or send a PR. ;-)