最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Load precompiled Handlebars template in Node script? - Stack Overflow

programmeradmin1浏览0评论

I have a simple Handlebars template at email-template.hbs that I'd like to precompile and load into my app.js file without reading from the filesystem and compiling it every time app.js is run.

Right now, I have something that looks like this:

var handlebars = require('handlebars');
var fs = require('fs');

var source = fs.readFileSync('./email-template.hbs', 'utf-8');
var template = handlebarspile(source);

I'd rather have something like this:

var handlebars = require('handlebars');
var template = require('email-template');

Where email-template.js is the precompiled email-template.hbs template.

I have a simple Handlebars template at email-template.hbs that I'd like to precompile and load into my app.js file without reading from the filesystem and compiling it every time app.js is run.

Right now, I have something that looks like this:

var handlebars = require('handlebars');
var fs = require('fs');

var source = fs.readFileSync('./email-template.hbs', 'utf-8');
var template = handlebars.compile(source);

I'd rather have something like this:

var handlebars = require('handlebars');
var template = require('email-template');

Where email-template.js is the precompiled email-template.hbs template.

Share Improve this question asked Aug 31, 2015 at 17:43 dstaleydstaley 1,0421 gold badge14 silver badges36 bronze badges 1
  • handlebarsjs.com/precompilation.html – robertklep Commented Aug 31, 2015 at 19:08
Add a comment  | 

2 Answers 2

Reset to default 24

I'm new to Node and Handlebars, and had the same question.

The trick is to precompile your template using the -c flag (which precompiles to Node's CommonJS module format, and gives it the path to the handlebars runtime module it needs).

Given you've already followed the directions for setting up precompilation (npm install handlebars -g), then for your example of generating email-template.js from ./email-template.hbs, try running this on the command line:

handlebars ./email-template.hbs -f ./email-template.js -c handlebars/runtime 

...which should produce an email-template.js with

var Handlebars = require("handlebars/runtime");  var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};

at the top. Then you can use it in your app.js like so:

var Handlebars = require('handlebars/runtime');
var hbtemplate = require('./email-template.js');

// You don't even seem to need the "var hbtemplate =" above, 
// as precompiling puts your template into the Handlebars object.
var template = Handlebars.templates['email-template'];
// ...then you can use template(data) to generate the HTML string

Just make yourself a helper function:

// requireHbs.js
var handlebars = require('handlebars');
var fs = require('fs');
var memoize = require('lodash.memoize');
module.exports = memoize(function requireHbs(templateName){
  var filename = './' + templateName + '.hbs';
  var source = fs.readFileSync(filename, 'utf-8');
  return handlebars.compile(source);
});

Then use it like this:

// app.js
var requireHbs = require('./requireHbs');
var template = requireHbs('email-template');
发布评论

评论列表(0)

  1. 暂无评论