npm

Need private packages and team management tools?Check out npm Orgs. »

gana-compile

1.0.1 • Public • Published

gana-compile npmjs.com The MIT License npm downloads

Pretty small synchronous template engine built on ES2015 Template Strings, working on node@0.10 too. No RegExps, support for helpers and what you want. Use gana if you wanna both async and sync support.

code climate standard code style travis build status coverage status dependency status

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 RegExps 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 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 (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 accepts locals object to populate the template.

Params

  • template {String}: string to compile to a function
  • returns {Function}: like compileFn(locals), where locals must be object
  • throws {TypeError}: if template not a string
  • throws {TypeError}: if locals not an object
  • throws {ReferenceError}: if key not exists in locals 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

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.

Charlike Make Reagent new message to charlike freenode #charlike

tunnckoCore.tk keybase tunnckoCore tunnckoCore npm tunnckoCore twitter tunnckoCore github

install

npm i gana-compile

Downloadsweekly downloads

15

version

1.0.1

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
  • avatar
Report a vulnerability