在Node脚本中加载预编译的Handlebars模板?

时间:2015-08-31 17:43:55

标签: javascript node.js handlebars.js

我在compile - Classpath for compiling the main sources. +--- project :library_refreshaction_appcompat +--- project :library_swipelistview | +--- com.nineoldandroids:library:2.4.0 | \--- com.android.support:support-v4:23.0.0 | \--- com.android.support:support-annotations:23.0.0 +--- project :library_numberpicker +--- project :library - viewpageindicator2.4 | \--- com.android.support:support-v4:23.0.0 (*) +--- project :library_handmark_pull +--- project :library_enhancedlistview | \--- com.nineoldandroids:library:2.4.0 +--- project :library_photoView +--- project :library +--- project :library_imageSlider | +--- com.android.support:support-v4:23.0.0 (*) | +--- com.nineoldandroids:library:2.4.0 | \--- com.squareup.picasso:picasso:2.3.4 +--- project :library_cards +--- project :library_animations | \--- project :library_easing_animation | \--- com.nineoldandroids:library:2.4.0 +--- project :library_PagerSliding | \--- com.android.support:support-v4:23.0.0 (*) +--- project :library_ParallaxScroll +--- project :library_HeaderListView | \--- com.android.support:appcompat-v7:23.0.0 | \--- com.android.support:support-v4:23.0.0 (*) +--- project :library_SwitchButton +--- project :listviewanimation-core | +--- com.nineoldandroids:library:2.4.0 | \--- com.android.support:support-v4:23.0.0 (*) +--- com.nineoldandroids:library:2.4.0 +--- com.squareup.picasso:picasso:2.3.4 +--- com.commonsware.cwac:endless:1.2.3 | \--- com.commonsware.cwac:adapter:1.0.1 +--- com.commonsware.cwac:merge:1.1.1 | \--- com.commonsware.cwac:sacklist:1.0.+ -> 1.0.2 +--- com.google.android.gms:play-services-maps:7.8.0 | \--- com.google.android.gms:play-services-base:7.8.0 | \--- com.android.support:support-v4:22.2.0 -> 23.0.0 (*) +--- com.google.android.gms:play-services-analytics:7.8.0 | \--- com.google.android.gms:play-services-base:7.8.0 (*) +--- com.android.support:support-v4:23.0.0 (*) +--- com.android.support:appcompat-v7:23.0.0 (*) +--- com.android.support:recyclerview-v7:23.0.0 | +--- com.android.support:support-annotations:23.0.0 | \--- com.android.support:support-v4:23.0.0 (*) +--- com.android.support:cardview-v7:23.0.0 +--- com.squareup.retrofit:retrofit:1.9.0 | \--- com.google.code.gson:gson:2.3.1 +--- com.facebook.android:facebook-android-sdk:4.5.0 | +--- com.parse.bolts:bolts-android:1.2.0 | \--- com.android.support:support-v4:[22,23) -> 23.0.0 (*) +--- com.nostra13.universalimageloader:parent:1.9.4 +--- com.github.rampo.updatechecker:library:2.1.8 +--- com.google.zxing:core:3.2.1 +--- com.squareup.okio:okio:1.5.0 +--- de.hdodenhof:circleimageview:1.3.0 +--- com.loopj.android:android-async-http:1.4.8 +--- :sdk-1.2.0.2: \--- com.crashlytics.sdk.android:crashlytics:2.5.0 +--- io.fabric.sdk.android:fabric:1.3.5 +--- com.crashlytics.sdk.android:crashlytics-core:2.3.3 +--- com.crashlytics.sdk.android:answers:1.3.0 | \--- io.fabric.sdk.android:fabric:1.3.5 \--- com.crashlytics.sdk.android:beta:1.1.3 处有一个简单的Handlebars模板,我想预先编译并加载到我的email-template.hbs文件中,而无需从文件系统读取并在每次app.js时编译它运行

现在,我有一些看起来像这样的东西:

app.js

我宁愿这样:

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

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

var handlebars = require('handlebars'); var template = require('email-template'); 是预编译的email-template.js模板。

2 个答案:

答案 0 :(得分:16)

我是Node和Handlebars的新手,也有同样的问题。

诀窍是使用-c标志预编译模板(预编译为Node的CommonJS模块格式,并为其提供所需的手柄运行时模块的路径)。

鉴于您已经按照setting up precompilationnpm install handlebars -g)的说明进行操作,那么对于从email-template.js生成./email-template.hbs的示例,请尝试在命令行上运行此代码:

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

...应生成email-template.js

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

在顶部。然后您可以在app.js中使用它,如下所示:

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

答案 1 :(得分:0)

让自己成为帮手功能:

// 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);
});

然后像这样使用它:

// app.js
var requireHbs = require('./requireHbs');
var template = requireHbs('email-template');