What is dot?
The 'dot' npm package is a fast and lightweight template engine in JavaScript. It allows you to create dynamic HTML templates with embedded JavaScript logic. It is known for its simplicity and performance, making it suitable for both client-side and server-side rendering.
What are dot's main functionalities?
Basic Template Rendering
This feature allows you to create a basic template and render it with dynamic data. The template string contains placeholders that are replaced with actual data when the template is rendered.
const doT = require('dot');
const template = doT.template('Hello, {{=it.name}}!');
const result = template({ name: 'World' });
console.log(result); // Output: Hello, World!
Conditional Rendering
This feature allows you to include conditional logic within your templates. The template string can contain conditional statements that control whether certain parts of the template are rendered.
const doT = require('dot');
const template = doT.template('{{? it.show }}Hello, {{=it.name}}!{{?}}');
const result = template({ show: true, name: 'World' });
console.log(result); // Output: Hello, World!
Looping
This feature allows you to loop over arrays within your templates. The template string can contain loop statements that iterate over arrays and render each item.
const doT = require('dot');
const template = doT.template('{{~it.items :item:index}}<li>{{=item}}</li>{{~}}');
const result = template({ items: ['Item 1', 'Item 2', 'Item 3'] });
console.log(result); // Output: <li>Item 1</li><li>Item 2</li><li>Item 3</li>
Other packages similar to dot
ejs
EJS (Embedded JavaScript) is a simple templating language that lets you generate HTML markup with plain JavaScript. It is similar to 'dot' in that it allows embedding JavaScript logic within templates, but EJS is more feature-rich and widely used.
handlebars
Handlebars is a popular templating engine that provides a cleaner syntax for templates and includes features like partials and helpers. It is more powerful and flexible compared to 'dot', but also more complex.
mustache
Mustache is a logic-less templating engine that enforces a strict separation of logic and presentation. It is simpler and more restrictive compared to 'dot', making it easier to maintain but less flexible.
Created in search of the fast and concise JavaScript templating function with emphasis on performance under V8 and nodejs.
doT can be used with nodejs and other javascript environments.
doT.js is a blend of jQote2 and underscore.js templating functions with some additions.
jQote2 technique provides for fast performance and also enables flushing data to the stream at any point in the generated template function, this is handy when using on server.
Features:
custom delimiters,
runtime evaluation,
runtime interpolation,
compile-time evaluation,
encoding,
control whitespace - strip or preserve,
streaming friendly
Usage:
Default delimiters:
{{= }} for interpolation,
{{ }} for evaluation,
{{! }} for interpolation with encoding,
{{# }} for compile-time evaluation/includes,
{{# #}} for compile-time defines
Other delimiters may be configured.
To control whitespace use 'strip' option, true - to strip, false - to preserve.
Sample:
// 1. Compile template function
var tempFn = doT.template("<h1>Here is a sample template {{=it.foo}} </h1>");
// 2. Use template function as many times as you like
var resultText = tempFn({foo: 'with doT'});
License:
Licensed under the MIT License. (See LICENSE-DOT)