Command-line interface for template compilation and packaging with support for multiple output formats and automated build processes.
Command-line tool for compiling doT templates from directories with flexible output options.
# Basic syntax
dottojs [options]
# Available options:
-s, --source [value] # Source folder/file path for templates
-d, --dest [value] # Destination folder for compiled files
-g, --global [value] # Global variable name for browser templates (default: "window.render")
-p, --package [value] # Package all templates into specified minified file
-h, --help # Display help information
-V, --version # Display version numberUsage Examples:
# Compile templates from views folder to output folder
dottojs -s ./views -d ./output
# Compile with custom global variable
dottojs -s ./templates -d ./dist -g "MyApp.templates"
# Compile and package into single minified file
dottojs -s ./src/templates -d ./dist -p ./dist/templates.min.js
# Compile from current directory to current directory
dottojs -s .
# Full example with all options
dottojs -s ./src/views -d ./dist/templates -g "App.render" -p ./dist/all-templates.min.jsThe CLI tool processes template files with the same behavior as the programmatic doT.process() function.
// CLI processing equivalent to:
doT.process({
path: sourceOption,
destination: destOption,
global: globalOption
});
// File type processing:
// .def files -> Template definitions/partials
// .dot files -> Compiled to functions (not output as files)
// .jst files -> Compiled to standalone .js filesProcessing Flow:
.def, .dot, and .jst files.def files as template partials.dot and .jst files.js files in destination directoryGenerated JavaScript files are compatible with multiple module systems.
// Generated file structure for .jst templates:
(function(){
var templateName = function(it) {
// Compiled template function
};
// Multi-format module export
if(typeof module!=='undefined' && module.exports) {
module.exports = templateName; // CommonJS
} else if(typeof define==='function') {
define(function(){return templateName;}); // AMD
} else {
window.render = window.render || {}; // Browser global
window.render['templateName'] = templateName;
}
}());Usage of Generated Files:
// CommonJS usage
const userTemplate = require('./dist/user.js');
const html = userTemplate({ name: "Alice", email: "alice@example.com" });
// AMD usage
define(['./dist/user'], function(userTemplate) {
return userTemplate({ name: "Bob", email: "bob@example.com" });
});
// Browser global usage
const html = window.render.user({ name: "Charlie", email: "charlie@example.com" });Example directory structure and CLI processing results.
Input Directory (./views):
views/
├── layout.def # Page layout partial
├── header.def # Header partial
├── footer.def # Footer partial
├── user.dot # User template (not output as file)
├── product.dot # Product template (not output as file)
├── newsletter.jst # Newsletter template -> newsletter.js
├── email.jst # Email template -> email.js
└── report.jst # Report template -> report.jsCLI Command:
dottojs -s ./views -d ./dist/templates -g "MyApp.templates"Output Directory (./dist/templates):
dist/templates/
├── newsletter.js # Standalone newsletter template
├── email.js # Standalone email template
└── report.js # Standalone report templateResult:
.def files loaded as partials (no output files).dot files compiled but not output as files.jst files compiled to standalone .js files{{#def.layout}}, {{#def.header}}, etc.The --package option combines and minifies all generated template files.
# Package all templates into single minified file
dottojs -s ./templates -d ./dist -p ./dist/all-templates.min.js
# This internally uses uglify-js to minify and combine filesPackaging Process:
.js files.js files from destination directory.js files remain in destination directoryPackage File Usage:
<!-- Browser usage -->
<script src="./dist/all-templates.min.js"></script>
<script>
// All templates available under configured global variable
const html = MyApp.templates.newsletter({ title: "Weekly Update" });
</script>The CLI tool provides error reporting for common issues.
# Directory creation errors
dottojs -s ./templates -d ./invalid/deep/path/dest
# Error: Cannot create destination directory
# Source directory not found
dottojs -s ./nonexistent -d ./dist
# Error: Source directory does not exist
# Template compilation errors
dottojs -s ./templates-with-syntax-errors -d ./dist
# Error: Template compilation failed for filename.jstCommon Error Solutions:
CLI tool can be integrated into build processes and package.json scripts.
{
"scripts": {
"build-templates": "dottojs -s ./src/templates -d ./dist/templates",
"build-templates-prod": "dottojs -s ./src/templates -d ./dist/templates -p ./dist/templates.min.js",
"watch-templates": "chokidar './src/templates/**/*.{dot,def,jst}' -c 'npm run build-templates'"
},
"devDependencies": {
"dot": "^1.1.3",
"chokidar-cli": "^2.1.0"
}
}Build Integration Examples:
# Development build
npm run build-templates
# Production build with packaging
npm run build-templates-prod
# Watch for changes during development
npm run watch-templates
# Integration with other build tools
gulp.task('templates', function() {
return exec('dottojs -s ./src/templates -d ./dist/templates');
});