Skip to content

wenxinwilliam/handlebars-template-loader

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

handlebars-template-loader

A Handlebars template loader for Webpack


###Usage
**Installation** ```bash $ npm install handlebars-template-loader ```
**Add loader** ```javascript module.exports = { //... loaders: [ //... { test: /\.hbs/, loader: "handlebars-template-loader" } ] //... node: { fs: "empty" //avoids error messages } }; ```
**Loading templates**
<!-- file: hello.hbs -->
<p>Hello&nbsp;{{name}}</p>
//file: app.js
var compiled = require('./hello.hbs');
return compiled({name: "world"});

**Include tag**
<!-- file: main.hbs -->
<p>Hello, <!--include message.hbs--></p>
<!-- file: message.hbs -->
<em>how are you?</em>

**Adding helpers**
//file: helpers.js

//get handlebars instance
var Handlebars = require('handlebars-template-loader').Handlebars;

Handlebars.registerHelper('list', function(items, options) {
  var out = "<ul>";

  for(var i=0, l=items.length; i<l; i++) {
    out = out + "<li>" + options.fn(items[i]) + "</li>";
  }

  return out + "</ul>";
});

Handlebars.registerHelper('link', function(text, url) {
  text = Handlebars.Utils.escapeExpression(text);
  url  = Handlebars.Utils.escapeExpression(url);

  var result = '<a href="' + url + '">' + text + '</a>';

  return new Handlebars.SafeString(result);
});
//file: main.js

//include app helpers
require("./helpers.js");

###License

Released under the MIT license.

About

A Handlebars template loader for Webpack

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%