Automatic compilation system that processes template files from directories, supporting .dot, .def, and .jst file extensions with customizable output and module options.
Auto-compiles templates from filesystem with support for multiple file types and output formats.
/**
* Auto-compile templates from filesystem
* @param {object} options - Processing configuration
* @returns {object} Object containing compiled template functions from .dot files
*/
function process(options);
interface ProcessOptions {
path?: string; // Source path for templates (default: "./")
destination?: string; // Output path for compiled .js files (default: same as path)
global?: string; // Global variable for browser templates (default: "window.render")
rendermodule?: object; // Module object to populate with compiled templates
templateSettings?: object; // Custom template settings (defaults to doT.templateSettings)
}Usage Examples:
const doT = require("dot");
// Basic auto-processing
const templates = doT.process({ path: "./views" });
const result = templates.mytemplate({ data: "content" });
// Custom configuration
const customTemplates = doT.process({
path: "./src/templates",
destination: "./dist/templates",
global: "MyApp.templates",
templateSettings: {
varname: "model",
strip: false
}
});
// Process with existing module object
const myModule = {};
doT.process({
path: "./templates",
rendermodule: myModule
});
// Now myModule contains compiled template functionsThe auto-processing system handles three types of template files with different compilation behaviors.
Compiled into functions and returned as properties of the result object.
// .dot files are compiled to functions accessible as object properties
// Example: mytemplate.dot becomes templates.mytemplate()Example .dot file (user.dot):
<div class="user">
<h2>{{=it.name}}</h2>
<p>Email: {{!it.email}}</p>
{{?it.admin}}
<span class="admin-badge">Admin</span>
{{?}}
</div>Usage:
const templates = doT.process({ path: "./views" });
const userHtml = templates.user({
name: "John Doe",
email: "john@example.com",
admin: true
});Template definitions/partials that can be included in other templates.
// .def files define reusable template parts
// Included via {{#def.filename}} syntaxExample .def file (header.def):
<header>
<h1>{{=it.title}}</h1>
<nav>{{=it.navigation}}</nav>
</header>Usage in template:
{{#def.header}}
<main>{{=it.content}}</main>Compiled into standalone .js files that can be loaded as CommonJS, AMD modules, or global variables.
// .jst files are compiled to standalone JavaScript files
// Can be loaded with require(), AMD define(), or as global variablesExample .jst file (product.jst):
<div class="product">
<h3>{{=it.name}}</h3>
<p class="price">${{=it.price}}</p>
{{?it.onSale}}
<span class="sale">On Sale!</span>
{{?}}
</div>Generated output (product.js):
(function(){
// Compiled template function
var product = function(it) {
// Generated template code
};
// Module export handling
if(typeof module!=='undefined' && module.exports) {
module.exports = product;
} else if(typeof define==='function') {
define(function(){return product;});
} else {
window.render = window.render || {};
window.render['product'] = product;
}
}());The auto-processing system scans the specified directory for template files.
// Processing behavior:
// 1. Scans directory for .def, .dot, .jst files (ignores subdirectories)
// 2. Loads all .def files first as template definitions
// 3. Compiles .dot files to functions in returned object
// 4. Compiles .jst files to standalone .js files in destination folderDirectory structure example:
views/
├── layout.def // Partial for page layout
├── header.def // Partial for header
├── user.dot // User template (returns function)
├── product.dot // Product template (returns function)
├── newsletter.jst // Newsletter template (creates .js file)
└── email.jst // Email template (creates .js file)Processing result:
const templates = doT.process({ path: "./views" });
// templates.user() and templates.product() are available
// newsletter.js and email.js files created in destination folderSupport for reusable template parts with parameter passing.
// Definition syntax in .def files or inline definitions
// {{##def.name:parameter:template_content#}}
// {{##def.name:=template_content#}}
// Usage syntax
// {{#def.name}} // Include without parameters
// {{#def.name:variable}} // Include with parameterExample with parameterized partials:
.def file (card.def):
{{##def.card:item:
<div class="card">
<h3>{{=item.title}}</h3>
<p>{{=item.description}}</p>
</div>
#}}Usage in template:
{{~it.items:item}}
{{#def.card:item}}
{{~}}Auto-processing provides logging and error handling for file operations.
const doT = require("dot");
// Enable/disable logging
doT.log = true; // or false
try {
const templates = doT.process({ path: "./invalid-path" });
} catch (error) {
console.error("Processing failed:", error.message);
}Log output example:
Compiling all doT templates...
Loaded def header
Loaded def footer
Compiling user.dot to function
Compiling product.dot to function
Compiling newsletter.jst to file