npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lodash-template-webpack-loader

v1.0.2

Published

Webpack loader for Lodash templates

Downloads

1,888

Readme

lodash-template-webpack-loader

Build Status

Webpack loader for Lodash templates

Supports the features you probably expect, such as escaping and template imports, but without the kitchen sink. Unless you want the kitchen sink!

Installation

npm install --save-dev lodash-template-webpack-loader

Usage

Set up the loader in your webpack.config.js:

module.exports = {
    //...
    module: {
        loaders: [{
            test: /(\.tpl|\.html)$/,
            loader: 'lodash-template-webpack',
        }],
    },
    lodashTemplateLoader: {
        // optional configuration...
    },
    //...
};

This will allow you to require and use templates like this:

var template = require('./path/to/template.tpl');
var html = template({
    title: 'Hello world',
});

Configuration

By default, you get variable interpolation, escaping (_.escape), and evaluation for free (<%= thing %>, <%- escapedThing %>, and <% if (thing) { %>...<% } %>, respectively). You do not get all of Lodash in the _ variable or _.templateSettings.imports.

There are a number of configuration options available:

  • globalLodash: enables the assumption of a global _ variable
  • engine: specifies the library to use for the local _ variable in the template (ie. var _ = require(engine))
  • templateEscape: set to false to disable automatic inclusion of _.escape
  • imports
  • importsModule
  • interpolate
  • escape
  • evaluate
  • variable
  • prependFilenameComment
  • attributes
  • root
  • parseDynamicRoutes

The _ variable

If you already include the Lodash kitchen sink globally, include globalLodash: true in your config. _ will be presumed to be available for use in your templates.

If you want the Lodash kitchen sink, but would rather define _ locally, use engine: 'lodash'in your configuration. You can use this to specify 'underscore', if you want.

Escaped variable printing

Even if you don't include all of Lodash, this loader includes _.escape by default so that escaped printing works with no configuration. If you don't want that behavior, add templateEscape: false and <%- %> will break.

templateEscape does nothing you're using the globalLodash or engine options.

Template imports

_.templateSettings.imports provides default variables that are imported into the compiled 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.

If you use globalLodash or engine, it will use the value of _.templateSettings.imports at the time you require the template.

template.tpl
"<%- scream(sentence) %>", he said with an exaggerated smile.
module.js
_.templateSettings.imports.scream = function(str) {
    return (str || '').toUpperCase();
};
var template = require('./path/to/template.tpl');
template('I love dogs.');

Setting _.templateSettings.imports AFTER require-ing the template will not work!

You can specify a string of raw javascript for the imports option to use instead:

module.exports = {
    // ...
    lodashTemplateLoader: {
        imports: '{title: \'Hi, world!\'}',
    },
    // ...
};

In some cases, it may be preferable to specify a module to use for the imports. importsModule lets you specify module that exports the template imports.

module.exports = {
    // ...
    lodashTemplateLoader: {
        importsModule: path.join(__dirname, 'template-imports'),
    },
    // ...
};

Template settings

You can modify the regular expressions that Lodash uses for interpolate, escape, and evaluate:

module.exports = {
    // ...
    lodashTemplateLoader: {
        interpolate: '\\{\\[(.+?)\\]\\}',
        escape: '\\{\\{(.+?)\\}\\}',
        evaluate: '\\{%([\\s\\S]+?)%\\}',
    },
    // ...
};

You can also specify the variable option, which can be used to namespace the variables you pass into the template:

module.exports = {
    // ...
    lodashTemplateLoader: {
        variable: 'data',
    },
    // ...
};
template.tpl
<h1><%- data.title %></h1>
<p><%- data.description %></p>
module.js
var template = require('./path/to/template.tpl');
template({
    title: 'Namespacing',
    description: 'Safe and sound!',
});

Prepending filename comment

When debugging a large single page app with the DevTools, it can be 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. <!-- view/user/edit.html -->).

module.exports = {
    // ...
    lodashTemplateLoader: {
        prependFilenameComment: __dirname,
    },
    // ...
};

Attribute parsing

This loader allows for parsing of URLs found in certain tag attributes. Matched attributes pass the URLs through the corresponding Webpack loader, allowing you to do things like remap or add hashes to image URLs. This is enabled for images by default, but you can add wherever using an array of tag:attribute pairs:

module.exports = {
    // ...
    module: {
        loaders: [{
            test: /(\.tpl|\.html)$/,
            loader: 'lodash-template-webpack',
        }, {
            test: /(\.gif|\.png|\.jpg)$/,
            loader: 'url-loader', // or 'file-loader'
        }],
    },
    lodashTemplateLoader: {
        attributes: ['img:src', 'link:href'],
    },
    // ...
};

You can specify a root to translate absolute paths to a relative location:

module.exports = {
    // ...
    lodashTemplateLoader: {
        root: 'assets',
    },
    // ...
};
<!-- Attribute in template -->
<img src="/img/franny.jpg">

<!-- Attribute after compilation -->
<img src="assets/img/franny.jpg">

You can specify root with parseDynamicRoutes: true to prepend a root to absolute URL attributes that use template expressions:

module.exports = {
    // ...
    lodashTemplateLoader: {
        root: '/assets/img',
        parseDynamicRoutes: true,
    },
    // ...
};
<!-- Attribute in template -->
<img src="/<%- image %>.jpg">

<!-- Attribute after compilation, invoked with `template({image: 'franny'})` -->
<img src="/assets/img/franny.jpg">

If you'd like to disable attribute parsing behavior entirely, simply disable attributes in your config:

module.exports = {
    // ...
    lodashTemplateLoader: {
        attributes: false,
    },
    // ...
};

License

MIT License


This project is inspired and based on underscore-template-loader. I owe the contributors on that project a lot of credit for the excellent work they've done, so thanks!