Express.js hbs模块 – 从.hbs文件注册部分

我正在使用express.js中的handlebars.js hbs包装 。 我有模板工作正常,但我需要添加部分来呈现与我的意见。

我想要做这样的事情:

hbs.registerPartial('headPartial', 'header'); // where "header" is an .hbs file in my views folder 

然而,它是抛出一个“标题部分无法find”。

我可以使registerPartial工作,如果我传递一个string的HTML到第二个参数,但我想为我的分支使用单独的视图文件。

我还没有find任何文件,但希望我可能会错过简单的东西。

有谁知道如何使用registerPartial方法中的视图文件? 如果是的话,我该如何实现呢?

UPDATE

为了给更多的上下文,让我添加更多的代码。 这是我的“服务器”文件 – app.js

 var express = require('express') , routes = require('./routes') , hbs = require('hbs'); var app = module.exports = express.createServer(); // Configuration app.configure(function(){ app.set('views', __dirname + '/views'); app.set('view engine', 'hbs'); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(__dirname + '/public')); }); app.configure('development', function(){ app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); }); app.configure('production', function(){ app.use(express.errorHandler()); }); // this is the line that generates the error hbs.registerPartial('headPartial', 'header'); // What I'm expecting is for "headPartial" to be a compiled template partial // of the template within views/header.hbs, but it is not loading this way. // If I do something like hbs.registerPartial('headPartial', '<p>test</p>'); // then it does work. I need to know how to pass an .hbs file to the // registerPartial method. // Routes app.get('/', routes.index); app.listen(3000); 

这里是我的routes.index文件:

 exports.index = function(req, res){ res.render('index', { title: 'Express' }) }; 

在我的视图文件夹中,我有三个模板:

 views/ header.hbs (this is my partial) index.hbs layout.hbs 

在我的index.hbs文件中,我将“headPartial”部分称为:

 {{> headPartial}} 

任何帮助是极大的赞赏。

此代码将所有部分模板加载到一个目录中,并按文件名使其可用:

 var hbs = require('hbs'); var fs = require('fs'); var partialsDir = __dirname + '/../views/partials'; var filenames = fs.readdirSync(partialsDir); filenames.forEach(function (filename) { var matches = /^([^.]+).hbs$/.exec(filename); if (!matches) { return; } var name = matches[1]; var template = fs.readFileSync(partialsDir + '/' + filename, 'utf8'); hbs.registerPartial(name, template); }); 

为方便起见,registerPartials提供了一种快速的方法来加载特定目录中的所有部分:

 var hbs = require('hbs'); hbs.registerPartials(__dirname + '/views/partials'); 

从目录加载的部分是基于文件名进行命名的,其中空格和连字符用下划线字符replace:

 template.html -> {{> template}} template 2.html -> {{> template_2}} login view.hbs -> {{> login_view}} template-file.html -> {{> template_file}} 

干杯!

看起来像创build一个variables,并拉动模板代码手动工作:

 var hbs = require('hbs') , fs = require('fs') , headerTemplate = fs.readFileSync(__dirname + '/views/_header.hbs', 'utf8'); 

然后:

 hbs.registerPartial('headPartial', headerTemplate); 

对我来说,我有模板文件my-partial.hbs

然后我尝试通过以下方式访问它们:

 {{> my-partial }} 

但部分被存储在hbs作为my_partial不pipe文件名。

这是由于hbs版本3.1.0行218

 .slice(0, -(ext.length)).replace(/[ -]/g, '_').replace('\\', '/'); 

这是在自述

对我来说,我有一个function,如:

 var hbs = require('hbs'); var fs = require('fs'); var statupfunc = { registerHbsPartials : function(){ //this is run when app start hbs.registerPartials(__dirname + "/" + resource.src.views + '/partials'); }, registerOneHbsPartials : function(event){ //this is run for gulp watch if(event.type == 'deleted') { return; } var filename = event.path; var matches = /^.*\\(.+?)\.hbs$/.exec(filename); if (!matches) { return; } var name = matches[1]; var template = fs.readFileSync(filename, 'utf8'); hbs.registerPartial(name, template); } }; 

在应用程序启动时运行statupfunc.registerHbsPartials ,然后注册含有statupfunc.registerOneHbsPartials的gulp手表以注册新创build的部分

 gulp.task('watch', function() { gulp.watch(resource.src.views + '/partials/*.*', statupfunc.registerOneHbsPartials); });