# underscore-template-loader
**Repository Path**: sonofmagic/underscore-template-loader
## Basic Information
- **Project Name**: underscore-template-loader
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2022-01-07
- **Last Updated**: 2022-01-07
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
underscore-template-loader
==========================
An Underscore.js and Lodash template loader for Webpack
### Changelog
* 1.1: Support macros through `query` (thx @JimYan, @devlinjunker)
* 1.0: Loader now works with Webpack 4. Still a beta release.
### Installation
```bash
npm install underscore-template-loader
```
Make sure you have the `underscore` or `lodash` package installed.
### Usage
```javascript
module.exports = {
//...
module: {
loaders: [
{ test: /\.html$/, loader: "underscore-template-loader" }
]
},
};
```
#### Template engine
You can specify an engine to specify the library used when you call underscore methods inside the template if you don't want to rely on the global `_` that is used by default.
```javascript
module.exports = {
//...
module: {
loaders: [
{
test: /\.html$/,
loader: "underscore-template-loader",
query: {
engine: 'lodash',
}
}
]
}
};
```
#### Loading templates
```html
Hello <%=name%>
``` ```javascript var compiled = require('./hello.html'); return compiled({name: "world"}); ``` #### Prepending filename comment When debugging a large single page app with the DevTools, it's often hard to find the template that contains a bug. With the following config a HTML comment is prepended to the template with the relative path in it (e.g. ``). ```javascript module.exports = { //... module: { loaders: [ { test: /\.html$/, loader: "underscore-template-loader", query: { prependFilenameComment: __dirname, } } ] } }; ``` #### Template settings You can override the delimiters used to determine data to injected (HTML-escaped or not) or code to evaluate in the templates. ```javascript module.exports = { //... module: { loaders: [ //... { test: /\.html$/, loader: "underscore-template-loader", query: { interpolate: '\\{\\[(.+?)\\]\\}', evaluate: '\\{%([\\s\\S]+?)%\\}', escape: '\\{\\{(.+?)\\}\\}' } } ] } }; ``` #### Template imports [`_.templateSettings.imports`](https://lodash.com/docs#templateSettings-imports) automatically includes variables or functions in your templates. This is useful when you have utility functions that you want to make available to all templates without explicitly passing them in every time the template is used. ```html<%= greet(name) %>
``` ```javascript var _ = require('lodash'); // Imports must be defined before the template is required _.templateSettings.imports = { greet: function(name) { return 'Hello, ' + name + '!'; }, }; var compiled = require('./hello.html'); return compiled({name: "world"}); ``` This is enabled by default when `lodash` is the engine used, but can be explicitly toggled using `withImports` option. ```javascript module.exports = { //... module: { loaders: [ //... { test: /\.html$/, loader: "underscore-template-loader", query: { withImports: true, } } ] } }; ``` #### Images In order to load images you must install either the *file-loader* or the *url-loader* package. ```javascript module.exports = { //... module: { loaders: [ { test: /\.html$/, loader: "underscore-template-loader" }, { test: /\.jpg$/, loader: "file-loader" }, { test: /\.png$/, loader: "url-loader?mimetype=image/png" }, ] } }; ``` ```htmlLorem ipsum
@br(3)Sit amet
@nl() ``` #### Custom macros We can include additional macros by defining them in the webpack configuration file. Remember that the value returned by a macro is inserted as plain javascript, so in order to insert a custom text we need to use nested quotes. For example, let's say that we want a macro that includes a copyright string in our template. ```javascript // File: webpack.config.js const webpack = require('webpack'); module.exports = { // ... module: { loaders: { // ... { test: /\.html$/, loader: "underscore-template-loader" }, } }, plugins: [ // ... new webpack.LoaderOptionsPlugin({ options: { macros: { copyright: function () { return "'Copyright FakeCorp 2014 - 2018
'"; }, }, }, }), ], } ``` We then invoke this macro from within the template as usual. ```html ``` #### Disabling macros You can disable macros if you are a bit unsure about their usage or just simply want faster processing. This is achieved by setting the `parseMacros` options to false. ```javascript module.exports = { // ... module: { loaders: { // ... { test: /\.html$/, loader: "underscore-template-loader", query: { parseMacros: false } }, } } } ``` #### Arguments Macros can accept an arbitrary number of arguments of different types: boolean, strings, numbers an object literals are supported. ```javascript // File: webpack.config.js module.exports = { // ... module: { loaders: { // ... { test: /\.html$/, loader: "underscore-template-loader" }, } }, macros: { header: function (size, content) { return "'Lorem ipsum
@header(3, 'Contents')Sit amet
``` #### Escaping Macro expressions can be escaped with the `\` character. ```html @br(3) \@nl() @br() ``` Translates to ```html