This repository has been archived by the owner. It is now read-only.
No longer maintained! I recommend JSX and React
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

ekma: templating for ES6

npm install ekma --save

ekma is no longer maintained! I've moved on to using JSX and React variants for templating, as they handle escaping and composition with far less syntactic noise.

There are lots of templating languages and view systems for JS. Handlebars, Jade, Nunjucks, and React's JSX are just a few, and there are more every day! All of them expose a domain-specific language (DSL) and introduce plenty of complexity when all you want is regular JavaScript.

The common untapped feature is ES6 template strings, which ekma aims to fix in a portable and flexible way. ekma works well whether you're running native ES6 or transpiling to ES5 via Babel, Traceur, or TypeScript!

ekma includes TypeScript typings out of the box.


ekma requires an ES5 environment. That's it!


ekma templates are just functions that return string-like objects!

Use the $html template tag to automatically escape inputs to fit into HTML:

import { $html, $each, $if } from "ekma";

const myCollection = [5, 6, 7];

const template = () => $html`
	${ $if(myCollection.length === 0, () => `
		Nothing to show in the collection.
	`) }

	${ $each(myCollection, v => `
		Value: ${ v }
	`) }


The return value of the template is an object with a toString method and the property safeInHTML set to true.


This API is using TypeScript syntax for expressing types. For additional details, check the source of the module.

In these annotations, Stringable is an object that has a toString method, or is an actual string.

  • escapeHTML(html: string)
    • escapes the given string as HTML
  • $html
    • A template string tag that marks the string as HTML
    • Use $${ value } to output unescaped values in substitutions
    • Nested strings will automatically have correct escaping
  • $times(count: number, method: Function | Stringable)
    • Outputs the given Function or Stringable count times.
  • $map(collection: Array, callback: Function | Stringable)
    • Equivalent to, but evaluates functions
  • $each(collection: Array, callback: Function | Stringable)
    • Iterates over the collection and joins the resulting array into a string
  • $alias(object: any, method: Function)
    • Syntax sugar for method(object), used to shorten access chains
    • Similar to old JavaScript with
  • $if(condition: any, pass: Function | Stringable, fail: Function | Stringable)
    • An if statement in function/expression form
    • Returns the result of pass if the condition is true
    • Otherwise returns the result of fail
  • $join(collection: Stringable[])
    • Joins a collection of elements that may be safe in HTML


Building requires Node.js 5 or newer.

To build ekma from sources, run npm run build in the root of the project.

The main field specified in package.json is only valid after the library has been built, as compiled JS isn't checked into source control to reduce commit noise.


ekma is licensed under the MIT license. See for more details.