Awesome
gana
Small and powerful template engine with only sync and async compile. The mid-level between es6-template and gana-compile.
You might also be interested:
- es6-template - higher level than
gana
andgana-compile
, adds.render
and.compile
methods. (~1.5-2kb) - gana-compile - lower level than
gana
, which does only synchronous compile. (~1.1kb) - gana is just ~1.5kb minified, not gzipped.
Background
Uses the "bad" new Function
thing
I don't think that's a problem, because other template engines out there also uses some kind of eval
and it is used massively, believe. Most of them uses eval
, most of them uses with
, others of them uses RegExp
s and etc. They all are with custom non-standard delimiters. They do too much to accomplish same results as gana
. They requires too big codebase - and finally what, they still uses some of the "bad" things in JS.
In other side, gana-compile
(which is behind this package) uses only new Function and nothing more. The whole core logic thing is just 3-5 lines of code.
Biggest names uses "bad" things, too
Names such verb, update, templates, generate, assemble in our community uses engine / engine-base / engine-cache - engine
uses with
, new Function
and RegExp
. Not to mention some of the most famous "real" template engines with features like partials, helpers and etc. You can have partials and helpers here in gana
too. The most awesome thing of the engine
that is used by verb
and assemble
is they can have asynchronous helpers - that's awesome, really! But if you will use bad things anyway, the core logic can be done a lot easier, with a lot smaller codebase.
Tricking magic
Behind the scenes gana
/ gana-compile
uses ES2015 (ES6) template strings inside the bad new Function
which seems to work even in node@0.10
which don't have support for Template Strings! That's strange, but it works and give us that awesome and small codebase without any costs. You just pass normal string 'foo ${bar} and baz'
and then { bar: 'bar' }
in the returned function.
Note about standard (>= v8) users
Recently in standard was added rule that ban usage of ${}
in normal ''
strings. So be awere of that and add /* eslint-disable no-template-curly-in-string */
comment before your stuff to get things working without problems.
Install
npm i gana --save
Usage
For more use-cases see the tests
const gana = require('gana')
gana
Sync and async compile, using
${}
delimiters and ES2015 Template Strings. Workin' on node@0.10 too.
Params
<template>
{String}: template to compile.[cb]
{Function}: optional, function withcb(err, compileFn)
signature.returns
{Function}: if nocb
, returnscompileFn
that accept object.
Example
var gana = require('gana')
var template = 'Hello, ${ucfirst(author.name)}! Welcome in our ${place}.'
var locals = {
author: {
name: 'charlike'
},
place: 'club',
ucfirst: function ucfirst (val) {
return val.charAt(0).toUpperCase() + val.slice(1)
}
}
// sync
var compileFn = gana(template)
var result = compileFn(locals)
console.log(result)
// => 'Hello, Charlike! Welcome in our club.'
// asynchronous
gana(template, function callback (err, compileFn) {
if (err) return console.error(err)
var result = compileFn(locals)
console.log(result)
// => 'Hello, Charlike! Welcome in our club.'
})
Related
- async-helpers: Use async helpers in templates with engines that typically only handle sync… more | homepage
- engine-base: Default engine for Template. | homepage
- es6-template: Easy and small template engine for the browser and nodejs. | homepage
- gana-compile: Pretty small synchronous template engine built on ES2015 Template Strings, working on… more | homepage
- j140: Template engine in 140 bytes, by @jed Schmidt. Support helpers, partials and… more | homepage
- octet: 1kb template engine for the browser and nodejs. Support helpers, partials and… more | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.