doT.js module loader for webpack
Install using npm
.
doT is a peer dependency and should be installed separately:
npm install --save-dev dot dotjs-loader
Configure the loader in webpack.config.js
:
module: {
loaders: [{ test: /\.dot$/, loader: 'dotjs-loader' }];
}
... and then load templates using require()
:
const template = require('./template.dot');
// => compiles template.dot content as template function
console.log(template());
Or, using ES6 modules:
import template from './template.dot';
// template function is the default export
console.log(template());
Pass any template data to the function:
template({ key: 'value' });
Alternatively you can explicitly call the loader without having configured it:
const template = require('dotjs-loader!./file.dot');
The following options
are available.
They map directly to doT.templateSettings
unless stated otherwise:
evaluate
interpolate
encode
use
define
conditional
iterate
varname
strip
append
Options are most conveniently passed in webpack.config.js
:
module: {
loaders: [
{
test: /\.dot$/,
loader: 'dotjs-loader',
options: {
varname: 'context',
},
},
];
}
dot-loader
is another loader
for doT templates, but it doesn't allow configuration of templateSettings
.
At the time of writing there was a
PR to add this feature,
but it suffered from some drawbacks:
- it's not merged
- it takes configuration from a
.dotrc
file rather than loader options - it doesn't mark that file as a loader dependency
I've also taken the opportunity to include unit tests and hope to add more features as time permits.