Gathering detailed insights and metrics for handlebars-template-loader
Gathering detailed insights and metrics for handlebars-template-loader
Gathering detailed insights and metrics for handlebars-template-loader
Gathering detailed insights and metrics for handlebars-template-loader
handlebars-template-loader-fixer
A Handlebars template loader for Webpack
micro-tpl-loader
Micro mustache template loader for webpack
html-bundler-webpack-plugin
Generates complete single-page or multi-page website from source assets. Built-in support for Markdown, Eta, EJS, Handlebars, Nunjucks, Pug. Alternative to html-webpack-plugin.
helper-loader
Load template engine helpers (handlebars, lo-dash, etc) from file paths, globs, arrays or objects.
npm install handlebars-template-loader
Typescript
Module System
Node Version
NPM Version
JavaScript (96.29%)
HTML (3.71%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
45 Stars
68 Commits
11 Forks
1 Watchers
11 Branches
8 Contributors
Updated on Jun 25, 2025
Latest Version
1.1.0
Package Id
handlebars-template-loader@1.1.0
Unpacked Size
53.03 kB
Size
13.03 kB
File Count
54
NPM Version
10.9.2
Node Version
22.14.0
Published on
Jun 25, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
4
A Handlebars template loader for Webpack
Table of Contents
1npm install handlebars-template-loader
Since version 0.5.4, this loaders does not include Handlebars in its dependency list. Make sure to install Handlebars before running webpack. Read https://github.com/npm/npm/issues/6565 for details.
1module.exports = { 2 //... 3 4 module: { 5 loaders: [ 6 { test: /\.hbs/, loader: "handlebars-template-loader" } 7 ] 8 }, 9 10 node: { 11 fs: "empty" // avoids error messages 12 } 13};
1<!-- File: hello.hbs --> 2<p>Hello {{name}}</p>
1// File: app.js 2var compiled = require('./hello.hbs'); 3return compiled({name: "world"});
1// File: helpers.js 2 3// Get Handlebars instance 4var Handlebars = require('handlebars-template-loader/runtime'); 5 6Handlebars.registerHelper('list', function(items, options) { 7 var out = "<ul>"; 8 9 for(var i=0, l=items.length; i<l; i++) { 10 out = out + "<li>" + options.fn(items[i]) + "</li>"; 11 } 12 13 return out + "</ul>"; 14}); 15 16Handlebars.registerHelper('link', function(text, url) { 17 text = Handlebars.Utils.escapeExpression(text); 18 url = Handlebars.Utils.escapeExpression(url); 19 20 var result = '<a href="' + url + '">' + text + '</a>'; 21 22 return new Handlebars.SafeString(result); 23});
1// File: main.js 2 3// Include template helpers 4require("./helpers.js");
1 // Get Handlebars instance 2 var Handlebars = require('handlebars-template-loader/runtime'); 3 4 // Require partial 5 var partial = require('path/to/my/_partial.hbs'); 6 7 // Register partial 8 Handlebars.registerPartial('my_partial_name', partial); 9
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. <!-- view/user/edit.html -->
).
1module.exports = { 2 //... 3 4 module: { 5 loaders: [ 6 { 7 test: /\.hbs$/, 8 loader: "handlebars-template-loader", 9 query: { 10 prependFilenameComment: __dirname, 11 } 12 } 13 ] 14 } 15};
In order to load images you must install either the file-loader
or the url-loader
package.
1module.exports = { 2 //... 3 4 module: { 5 loaders: [ 6 //... 7 { test: /\.hbs/, loader: "handlebars-template-loader" }, 8 { test: /\.jpg/, loader: "file-loader" }, 9 { test: /\.png/, loader: "url-loader?mimetype=image/png" }, 10 ] 11 } 12};
1<!-- Require image using file-loader --> 2<img src="img/portrait.jpg"> 3 4<!-- Require image using url-loader --> 5<img src="img/icon.png">
Images with an absolute path are not translated unless a root
option is defined
1<!-- Using root = undefined => no translation --> 2<img src="/not_translated.jpg"> 3 4<!-- Using root = 'images' => require('images/image.jpg') --> 5<img src="/image.jpg">
In order to deactivate image processing define the attributes
option as an empty array.
1module.exports = { 2 //... 3 4 module: { 5 loaders: [ 6 { 7 test: /\.hbs$/, 8 loader: "handlebars-template-loader", 9 query: { 10 attributes: [] 11 } 12 } 13 ] 14 } 15};
You could also add which attributes need to be processed in the form of pairs tag:attribute.
1module.exports = { 2 //... 3 4 module: { 5 loaders: [ 6 { 7 test: /\.hbs$/, 8 loader: "handlebars-template-loader", 9 query: { 10 attributes: ['img:src', 'x-img:src'] 11 } 12 } 13 ] 14 } 15};
Dynamic attributes won't be afected by this behaviour by default.
1<!-- Ignore "root" argument if attribute contains a template expression --> 2<img src="/img/{{doge}}.png" class="doge-img">
In order to append the root directory you'll need to specify the parseDynamicRoutes
argument.
1module.exports = { 2 //... 3 4 module: { 5 loaders: [ 6 { 7 test: /\.html$/, 8 loader: "handlebars-template-loader", 9 query: { 10 root: "myapp", 11 parseDynamicRoutes: true 12 } 13 } 14 ] 15 } 16};
1<!-- Attribute now translates to "myapp/img/{{doge}}.png" --> 2<img src="/img/cat-<%- currentCat.url %>.png" class="doge-img">
If you have a custom location for your Handlebars runtime module then you can set that in your query
object via the runtimePath
property. This is the path to the Handlebars runtime that every .hbs
file will require and use. By default this loader looks up the absolute path to the handlebars/runtime
in your node_modules
folder. Changing this property is useful if you are doing somethign non-standard with your Handlebar templates, for example setting an alias for the handlebars/runtime
path.
1module.exports = { 2 //... 3 4 module: { 5 loaders: [ 6 { 7 test: /\.html$/, 8 loader: "handlebars-template-loader", 9 query: { 10 runtimePath: 'handlebars/runtime' 11 } 12 } 13 ] 14 } 15};
Handlebars does support additional compilation options that you can specify in your query
object literal.
1module.exports = { 2 //... 3 4 module: { 5 loaders: [ 6 { 7 test: /\.html$/, 8 loader: "handlebars-template-loader", 9 query: { 10 root: "myapp", 11 strict: true, 12 noEscape: true 13 } 14 } 15 ] 16 } 17};
Macros allow additional features like including templates or inserting custom text in a compiled templates.
The require
macro expects a path to a handlebars template. The macro is then translated to a webpack require expression that evaluates the template using the same arguments.
1<h4>Profile</h4> 2 3Name: <strong>{{name}}</strong> 4<br/> 5Surname: <strong>{{surname}}</strong> 6<div class="profile-details"> 7 @require('profile-details.hbs') 8</div>
While the require
macro expects a resource that returns a function, the include
macro can be used for resources that return plain text. For example, we can include text loaded through the html-loader
directly in our template.
1<div class="wiki"> 2 <h3>Introduction</h3> 3 @include('intro.htm') 4 <h3>Authors</h3> 5 @include('authors.htm') 6</div>
The repeat
macro will repeat the given string the amount of times as specified by the second argument (default to 1). It will only accept string literals.
1<p>Lorem ipsum</p> 2@repeat('<br/>', 3) 3<p>Sit amet</p> 4@repeat('\n')
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.
1// File: webpack.config.js 2module.exports = { 3 // ... 4 5 module: { 6 loaders: { 7 // ... 8 { test: /\.hbs/, loader: "handlebars-template-loader" }, 9 } 10 }, 11 12 macros: { 13 copyright: function () { 14 return "'<p>Copyright FakeCorp 2014 - 2015</p>'"; 15 } 16 } 17}
We then invoke our macro from within the template as usual.
1<footer> 2 @copyright() 3</footer>
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.
1module.exports = { 2 // ... 3 4 module: { 5 loaders: { 6 // ... 7 { 8 test: /\.hbs/, 9 loader: "handlebars-template-loader", 10 query: { 11 parseMacros: false 12 } 13 }, 14 } 15 } 16}
Macros can accept an arbitrary number of arguments. Only boolean, strings and numeric types are supported.
1// File: webpack.config.js 2module.exports = { 3 // ... 4 5 module: { 6 loaders: { 7 // ... 8 { test: /\.html$/, loader: "handlebars-template-loader" }, 9 } 10 }, 11 12 macros: { 13 header: function (size, content) { 14 return "'<h" + size + ">" + content + "</h" + size + ">'"; 15 } 16 } 17}
1@header(1, 'Welcome') 2<p>Lorem ipsum</p> 3@header(3, 'Contents') 4<p>Sit amet</p>
Macro expressions can be escaped with the \
character.
1@repeat('<br/>', 3) 2\@escaped() 3@custom_macro()
Translates to
1<br/><br/><br/> 2@escaped() 3custom string
Released under the MIT license.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
6 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 5
Reason
Found 4/30 approved changesets -- score normalized to 1
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More