Fast JavaScript templating engine with custom delimiters, runtime evaluation, and auto-compilation support
npx @tessl/cli install tessl/npm-dot@1.1.0doT is a fast, concise JavaScript templating engine designed for high performance under V8 and Node.js. It provides flexible templating with custom delimiters, runtime evaluation, conditionals, iteration, partials, and encoding support. doT can be used as a logic-less or logic-enabled templating system with zero dependencies.
npm install dotconst doT = require("dot");For browser usage:
<script src="doT.js"></script>
<!-- doT is available as global variable -->const doT = require("dot");
// Simple template compilation
const template = doT.template("Hello {{=it.name}}!");
const result = template({ name: "World" });
console.log(result); // "Hello World!"
// Auto-processing templates from directory
const templates = doT.process({ path: "./views" });
const output = templates.mytemplate({ data: "content" });doT is built around several key components:
Core template compilation functionality for converting template strings into executable functions with custom delimiters and settings.
/**
* Compile template string into executable function
* @param {string} tmpl - Template string with doT syntax
* @param {object} c - Optional template settings
* @param {object} def - Optional predefined template definitions
* @returns {function} Compiled template function
*/
function template(tmpl, c, def);
/**
* Simplified template compilation for legacy compatibility
* @param {string} tmpl - Template string
* @param {object} def - Optional template definitions
* @returns {function} Compiled template function
*/
function compile(tmpl, def);Automatic compilation system that processes template files from directories, supporting .dot, .def, and .jst file extensions with customizable output options.
/**
* Auto-compile templates from filesystem
* @param {object} options - Processing configuration
* @returns {object} Object containing compiled template functions
*/
function process(options);
interface ProcessOptions {
path?: string; // Source path for templates (default: "./")
destination?: string; // Output path for compiled .js files
global?: string; // Global variable for browser templates
rendermodule?: object; // Module object to populate
templateSettings?: object; // Custom template settings
}Complete template syntax supporting interpolation, encoding, conditionals, iteration, partials, and custom delimiters.
// Template syntax patterns (configurable via templateSettings)
const templateSettings = {
evaluate: /\{\{([\s\S]+?(\}?)+)\}\}/g, // {{code}}
interpolate: /\{\{=([\s\S]+?)\}\}/g, // {{=variable}}
encode: /\{\{!([\s\S]+?)\}\}/g, // {{!variable}}
conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g, // {{?condition}}...{{?}}
iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g, // {{~array:value:index}}...{{~}}
use: /\{\{#([\s\S]+?)\}\}/g, // {{#def.name}}
useParams: /(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*([\w$\.]+|\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})/g,
define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g, // {{##def.name:...#}}
defineParams: /^\s*([\w$]+):([\s\S]+)/
};HTML encoding utilities for secure output rendering with configurable encoding rules and options.
/**
* Generate HTML encoding function
* @param {boolean} doNotSkipEncoded - Whether to encode already encoded entities
* @returns {function} HTML encoding function
*/
function encodeHTMLSource(doNotSkipEncoded);Command-line interface for template compilation and packaging with support for multiple output formats.
# CLI usage
dottojs -s source_folder -d destination_folder -g global_variable -p package_fileLegacy simplified templating module for backward compatibility (deprecated - use doT.js instead).
// Legacy doU module (separate file in package)
const doU = require("dot/doU.js");
// doU object structure
const doU = {
version: "0.1.2",
templateSettings: {
begin: "{{", // Opening delimiter
end: "}}", // Closing delimiter
varname: "it" // Data variable name
},
template: function(tmpl, conf) // Simplified template compilation
};Note: doU.js is provided for legacy compatibility only. New projects should use the main doT API instead.
const doT = {
name: "doT",
version: "1.1.1", // doT internal version (package version: 1.1.3)
templateSettings: {
evaluate: RegExp, // Pattern for code evaluation
interpolate: RegExp, // Pattern for variable interpolation
encode: RegExp, // Pattern for encoded output
use: RegExp, // Pattern for partials/includes
useParams: RegExp, // Pattern for parameterized partials
define: RegExp, // Pattern for template definitions
defineParams: RegExp, // Pattern for definition parameters
conditional: RegExp, // Pattern for conditional blocks
iterate: RegExp, // Pattern for iteration blocks
varname: "it", // Template data variable name
strip: true, // Strip whitespace
append: true, // Use append vs split mode
selfcontained: false, // Self-contained template mode
doNotSkipEncoded: false // Encode already encoded entities
},
log: true // Controls logging output
};