doT.js Templating Engine

1.1.3 · maintenance · verified Sun Apr 19

doT.js (pronounced "dot") is a lightweight, concise, and exceptionally fast JavaScript templating engine. It is designed to work seamlessly in both Node.js and browser environments, distinguishing itself by compiling templates into pure JavaScript functions, which contributes to its minimal overhead and high performance. The current stable release series is 1.x, with version 1.1.3 being the latest maintenance release, offering robust CommonJS and global browser compatibility. Development is active on version 2.x, currently in beta (2.0.0-beta.1), which introduces significant enhancements such as ES6 code generation, official TypeScript typings, and improved argument handling. doT.js maintains a conservative release cadence for major versions, prioritizing stability and efficiency. Its primary differentiators are its speed, minimal footprint, and lack of external dependencies, making it a strong choice for performance-sensitive applications where simplicity is key.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic template compilation and rendering using the `template` function, including both `it` context and multi-argument destructuring, as well as a more complex example with control flow.

import { template } from 'dot';

// Basic usage with 'it' context
const basicTmpl = template('Hello, {{=it.name}}!');
console.log(basicTmpl({ name: 'World' }));

// Usage with explicit argName (v2.x feature, fallback for v1.x)
const multiArgTmpl = template(
  'User ID: {{=id}}, Username: {{=username}}',
  { argName: ['id', 'username'] } // This option is 'varname' in v1.x
);
console.log(multiArgTmpl({ id: 123, username: 'jsuser' }));

// A more complex example with conditionals and loops
const listTmpl = template(`
  <h2>{{=it.title}}</h2>
  <ul>
    {{~it.items :item:idx}}
      <li>{{=idx + 1}}. {{=item}}</li>
    {{~}}
  </ul>
  {{? it.footer}}
    <p>{{=it.footer}}</p>
  {{?}}
`);

const data = {
  title: 'My Shopping List',
  items: ['Apples', 'Milk', 'Bread'],
  footer: 'Happy shopping!'
};
console.log(listTmpl(data));

view raw JSON →