gana-compile
v1.0.1
Published
Pretty small synchronous template engine built on ES2015 Template Strings, working on `[email protected]` too. No RegExps, support for helpers and what you want. Use [gana][] if you wanna both async and sync support.
Downloads
17
Readme
gana-compile
Pretty small synchronous template engine built on ES2015 Template Strings, working on
[email protected]
too. No RegExps, support for helpers and what you want. Use gana if you wanna both async and sync support.
You might also be interested in es6-template (>= v2), which is higher level of gana-compile
adding async support, .render
and .compile
methods.
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-compile
. They requires too big codebase - and finally what, they still uses some of the "bad" things in JS.
Biggest names uses "bad" things too
Names such verb, update, templates, generate, assemble in our community uses engine - respectively engine-base and/or engine-cache. 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-compile
too.
Tricking magic
Behind the scenes gana-compile
uses ES2015 (ES6) template strings inside the bad new Function
which seems to work even in [email protected]
which don't have support for Template Strings! That's strange, but it works and give us that awesome and small codebase (1.39kb, minified and not gzipped) - 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 standard 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-compile --save
Usage
For more use-cases see the tests
const ganaCompile = require('gana-compile')
ganaCompile
Compiles a
template
to a function, which acceptslocals
object to populate the template.
Params
template
{String}: string to compile to a functionreturns
{Function}: likecompileFn(locals)
, wherelocals
must beobject
throws
{TypeError}: iftemplate
not a stringthrows
{TypeError}: iflocals
not an objectthrows
{ReferenceError}: if key not exists inlocals
object
Example
var ganaCompile = require('gana-compile')
var template = 'Welcome here, ${ucfirst(name)}! And have fun!'
var locals = {
name: 'charlike',
ucfirst: function ucfirst (val) {
return val.charAt(0).toUpperCase() + val.slice(1)
}
}
var fn = ganaCompile(template)
var str = fn(locals)
console.log(str)
// => 'Welcome here, Charlike! And have fun!'
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
- 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.