platter

dead simple micro templating pre-compiler

npm install platter
9 downloads in the last week
22 downloads in the last month

Build Status

Micro template pre-compiler powered by underscore.js

Simple underscore.js template precompiler By default, it compiles to CommonJS Module format.

Micro Templating decendent doesn't require any library at runtime. So you don't need neither underscore.js nor lodash once you pre-compile your templates.

Basic Uaages

<div><%= hoge %></div>

will be simply

/*** template source *******
<div><%= hoge %></div>
**** template source end **/
module.exports = function(obj){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
with(obj||{}){
__p+='<div><%= hoge %></div>\n';
}
return __p;
}

Try

$ echo '<div><%= hoge %></div>' | platter

You can output to AMD format with --amd option.

$ echo '<div><%= hoge %></div>' | platter --amd

It'll output

/*** template source *******
<div><%= hoge %></div>
**** template source end **/
define(function () {
return function(obj){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
with(obj||{}){
__p+='<div><%= hoge %></div>\n';
}
return __p;
}});

You may want to use with --variable option

$ echo '<div><%= data.hoge %></div>' | platter --variable data
/*** template source *******
<div><%= data.hoge %></div>
**** template source end **/
module.exports = function(data){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
__p+='<div>'+
((__t=( data.hoge ))==null?'':__t)+
'</div>\n';
return __p;
}

With --variable option, there is no longer with(){}. It's preferable to specify variable name if you want performance.

Require

You may want to use underscore.js when you want to _.each(). In order to require modules, use --require option

hello.html

<% _.each(models, function(user) { %>
  Hello <%= user.firstName %> <%= user.lastName %>
<% }); %>
$ platter --require '_=underscore' hello.html
/*** template source *******
<% _.each(models, function(user) { %>
  Hello <%= user.firstName %> <%= user.lastName %>
<% }); %>
**** template source end **/
var _ = require("underscore");
module.exports = function(obj){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
with(obj||{}){
__p+='';
 _.each(models, function(user) {
__p+='\n  Hello '+
((__t=( user.firstName ))==null?'':__t)+
' '+
((__t=( user.lastName ))==null?'':__t)+
'\n';
 });
__p+='\n';
}
return __p;
}

You can require multiple modules.

$ platter --require '_=underscore,moment=moment' hello.html

If you want just each, you can use tiny module like component/each rather than underscore.js.

Options

-h, --help                         output usage information
-o, --out <dir>                    output directory path
-s, --src <dir>                    template source directory path
-r, --require <variable>=<module>  configuration
-w, --without-source               supress prepending source file
-s, --strict                       insert `"use strict";` for ECMAScript 5 strict mode
--amd                              output with Asynchronous Module Definition format
--variable <str>                   variable setting name
--evaluate <str>                   evaluate setting mather
--interpolate <str>                interpolate setting mather
--escape <str>                     escape setting mather
--settings <file>                  set settings by JavaScript or JSON

Examples

## compile "templates/hoge.html" to "lib/templates/hoge.js
$ platter < ./templates/hoge.html > ./lib/templates/hoge.js
or
$ platter ./templates/hoge.html > ./lib/templates/hoge.js
or
$ platter ./templates/hoge.html --out lib

## compile "public/templates/hoge.html" to public/js/templates/hoge.html
$ platter < ./public/templates/hoge.html > public/js/templates/hoge.js
or
$ platter ./public/templates/hoge.html > public/js/templates/hoge.js
or
$ platter ./public/templates/hoge.html --src public --out public/js
npm loves you