Home

Awesome

yeahjs

A tiny, modern, fast implementation of EJS (Embedded JavaScript Templates). A nearly drop-in replacement for ejs with a few intentional limitations.

Build Status Install Size Min-zipped Size Simply Awesome

Example

<ul>
<% for (let word of locals.items) { -%>
  <li><%= word %></li>
<% } -%>
</ul>
import {compile} from 'yeahjs';

const template = compile(ejs);
const output = template({items: ['flour', 'water', 'salt']});
<ul>
  <li>flour</li>
  <li>water</li>
  <li>salt</li>
</ul>

Compared to ejs

There are a few key differences that allow yeahjs to be so small and fast:

Otherwise yeahjs produces identical output to ejs.

Strict mode only

The with keyword has a very significant impact on performance in JavaScript, in addition to introducing hard to debug issues. Limiting yeahjs to strict mode makes sure it's always as fast and predictable as possible.

Static path includes

Static path includes make sure yeahjs can fully compile templates with includes at compile time, avoiding lazy compilation during template evaluation. This makes evaluation faster and more predictable.

Custom file handling

Not including any file-system-specific code makes yeahjs environment-agnostic, having the same bundle for both Node and the browsers and giving full control over how includes get read and resolved.

Usage

import {compile} from 'yeahjs';

const template = compile(ejs, options);

Returns a function of the form (data) => content. Options:

EJS cheatsheet

File handling

An example of using read, resolve and filename options in Node.js to process includes:

import {readFileSync} from 'fs';
import {join, dirname} from 'path';

const template = yeahjs.compile(`<%- include('../bar.html') %>`, {
    filename: 'foo/foo.html',
    resolve: (parent, filename) => join(dirname(parent), filename),
    read: filename => readFileSync(filename, 'utf8')
});