doT.js Templating Engine
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
-
ReferenceError: doT is not defined
cause Attempting to use `doT.template` directly in an environment where `doT` is not globally available or correctly imported/required.fixFor CommonJS (v1.x), ensure `const doT = require('dot');` is at the top of your file. For ESM (v2.x), use `import * as doT from 'dot';` or `import { template } from 'dot';` if only needing specific exports. -
TypeError: doT.template is not a function
cause This typically occurs when `doT` is imported incorrectly, resulting in `doT` being undefined or an object without the `template` property, or attempting to use `require` on an ESM-only build.fixVerify your import statement matches your module system (CommonJS `require` for v1.x, ESM `import` for v2.x). Ensure you're accessing `template` from the correct object returned by the import. -
Property 'varname' does not exist on type '{ argName: string[]; }'cause Using the deprecated `varname` option for template arguments with doT.js v2.x in a TypeScript environment.fixReplace `varname` with `argName` in your template options object to align with the v2.x API. Example: `{ argName: ['foo', 'bar'] }`.
Warnings
- breaking The template option `varname` was renamed to `argName` in v2.0.0-beta.1. Templates created with `varname` in v1.x will not work as expected if this option is used in v2.x.
- breaking Version 2.x of doT.js generates ES6 code by default and ships with official TypeScript typings, shifting towards ESM. While CJS is still supported, users migrating from v1.x (which is primarily CJS-focused) might need to adjust import statements for optimal compatibility.
- gotcha doT.js v2.0.0-beta.x versions are currently pre-release. They introduce new features and breaking changes, but should not be used in production environments without thorough testing due to potential instability or further API changes.
- gotcha Version 1.1.3 patched a code injection vulnerability related to prototype pollution (#291). Older versions are susceptible to this security issue.
Install
-
npm install dot -
yarn add dot -
pnpm add dot
Imports
- template
const doT = require('dot'); const template = doT.template;import { template } from 'dot'; - process
const doT = require('dot'); doT.process(...);import { process } from 'dot'; - DoTTemplateFunction
import type { DoTTemplateFunction } from 'dot';
Quickstart
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));