qiq
Minimal templating engine, based on minstache.
Installation
$ npm install qiq
qiq(1)
The qiq(1)
executable can compile a file to a valid
stand-alone commonjs module for you, there's no need to have qiq
as a dependency:
hello.qiq:
Hello {{name}}! {{^authenticated}}<a href="/login">login</a>{{/authenticated}}
convert it:
$ qiq < hello.qiq > hello.js
Now you can do:
var hello = ; var str = ; console; // => "Hello Tom! <a href="/login">login</a>
API
qiq(string, [obj], [opts])
Compile and render the given mustache string
with optional context obj
.
qiq.compile(string)
Compile the qiq string
to a stand-alone Function
accepting a context obj
.
Syntax
Variables
var qiq = ; var template = 'Hi {{name}}!'; ; // => "Hi Tommy!"; // nested objects also work var template = 'Hi {{name}}, how is your {{day.name}}?'; var data = name: 'Tommy' day: name: 'Tuesday' ; ; // => "Hello Tommy, how is your Tuesday?"; // to escape HTML, use a ! before your variable name var template = 'Good day {{!greeting}}'; ; // => "Good day <em>human</em>"; // you can also separate brackets and tokens with spaces, like: var template = 'Hi {{ name }}, how are you?'; ; // => "Hi Jerry, how are you?"; // and even use a different delimiter if you want to: var template = 'Howdy <% name %>!'; var opts = delimeter: /\<\% ?| ?\%\>/ ; ; // => "Howdy Jerry!";
Conditionals
// true var template = 'Hello.{{#foo}} Goodbye.{{/foo}}'; ; // => "Hello. Goodbye."; // truthy var template = 'Goodbye.{{#number}} Hello.{{/number}}'; ; // => "Goodbye. Hello."; // false var template = 'This is {{^bar}}not {{/bar}}a test.'; ; // => "This is not a test."; // if/else var template = 'Very {{#good}}good{{_else}}bad{{/good}}.'; ; // => "Very good."; // if/else reversed var template = 'Such {{^ugly}}nice{{_else}}ugly{{/ugly}}!'; ; // => "Such nice!"; // nested if/else! var template = 'Works? {{^works}}Nope.{{_else}}Yep, {{#awesome}}awesome{{_else}}cool{{/awesome}}!{{/works}}' ; // => "Works? Yep, cool!";
Functions
Your object can have functions that either return true or false, which are used to follow the template logic, or return a string in which case that is what is printed.
var template = '{{#isEmpty}} is empty {{_else}} not empty {{/isEmpty}}'; var obj = { return false } ; // => "not empty"; var template = '{{#toUpper}} these are letters {{/}}'; var obj = { return thunk } ; // => "THESE ARE LETTERS";
Arrays
var template = '<ul>{{#contacts}}<li>{{name}}</li>{{/contacts}}</ul>'; var list = contacts: name: 'tobi' name: 'loki' name: 'jane' ; ; // => "<ul><li>tobi</li><li>loki</li><li>jane</li></ul>";
License
MIT