art-template
art-template is a high-performance JavaScript template engine designed for both Node.js and browser environments. It achieves near-JavaScript-limit rendering speeds through a 'scope pre-declared' optimization technique. The current stable version is 4.13.4, with a release cadence that addresses bugs, performance, and adds minor features, as seen in recent patch and minor releases (e.g., v4.13.0 for performance fixes, v4.13.2 for type definitions). Key differentiators include its extreme speed, debugging friendliness (accurate error positioning, breakpoint support with Webpack loader), support for template inheritance, sub-templates, and a small browser footprint (6KB). It integrates with popular frameworks like Express and Koa, and build tools like Webpack.
Common errors
-
ReferenceError: window is not defined
cause A bug in `art-template` versions prior to `4.13.1` caused it to reference the global `window` object in non-browser environments, such as server-side rendering setups.fixUpgrade to `art-template@4.13.1` or higher to resolve the issue. -
Template compilation error: Helper 'someHelper' not found
cause Using the v3 `{{helper args}}` syntax for calling helper methods in `art-template` v4, which is no longer supported and throws a compilation error.fixRefactor template helper calls to use standard JavaScript expression syntax, e.g., `{{ helper.someHelper(args) }}` or `{{ someGlobalFunction(args) }}`. -
error TS2614: Module '"art-template"' has no exported member 'template'
cause Missing or incorrect TypeScript definition files (`index.d.ts`) were not shipped with `art-template` versions up to `4.13.1`, leading to TypeScript compilation errors.fixUpgrade `art-template` to version `4.13.2` or newer to ensure correct type definitions are included in your project.
Warnings
- breaking The `{{helper args}}` syntax for calling helper methods from art-template v3 is no longer compatible. Migrate to the standard JavaScript expression syntax for helpers.
- gotcha The `bail` option, which controls whether template compilation errors immediately throw an exception, now defaults to `true`. This means template errors will halt execution by default.
- gotcha TypeScript definition file (`index.d.ts`) was missing in versions up to `4.13.1`, leading to compilation errors or lack of type intelligence for TypeScript users.
- gotcha In some runtime environments, particularly those with a global `window` object but not fully compliant browser APIs (like certain server-side rendering setups), versions prior to 4.13.1 could throw 'window is not defined' errors.
Install
-
npm install art-template -
yarn add art-template -
pnpm add art-template
Imports
- template
import { template } from 'art-template';import template from 'art-template';
- template.defaults
import { defaults } from 'art-template';import template from 'art-template'; template.defaults.escape = false;
- template.compile
import { compile } from 'art-template';import template from 'art-template'; const render = template.compile(source);
Quickstart
import template from 'art-template';
// 1. Configure global defaults (optional)
template.defaults.escape = false; // Disable HTML escaping for raw output
// 2. Define a template string
const templateString = `
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>Hello, {{ name | capitalize }}!</h1>
<p>Your items:</p>
<ul>
{{ each items }}
<li>Item #{{ $index + 1 }}: {{ $value }}</li>
{{ /each }}
</ul>
{{ if showFooter }}
<footer>Rendered at: {{ date | dateFormat }}</footer>
{{ /if }}
<p>Raw HTML (if escape is disabled): {{ @rawHtml }}</p>
</body>
</html>
`;
// 3. Register a custom filter (optional)
template.defaults.imports.capitalize = (value: string) => {
return value.charAt(0).toUpperCase() + value.slice(1);
};
template.defaults.imports.dateFormat = (date: Date) => {
return date.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
};
// 4. Compile the template (recommended for performance in production)
const render = template.compile(templateString);
// 5. Define data
const data = {
title: 'Art Template Example',
name: 'world',
items: ['apple', 'banana', 'orange'],
showFooter: true,
date: new Date(),
rawHtml: '<strong>This is bold!</strong>'
};
// 6. Render the template with data
const output = render(data);
console.log(output);
// You can also render directly without pre-compiling for simple cases (less efficient)
const directOutput = template(templateString, {
title: 'Direct Render Example',
name: 'test',
items: ['one', 'two'],
showFooter: false,
date: new Date(),
rawHtml: '<i>Italic!</i>'
});
// console.log(directOutput);