swa*_*ins 28 node.js express handlebars.js
我在express.js中使用了handlebars.js hbs包装器.我有模板工作正常,但我需要添加部分以使用我的视图呈现.
我想做这样的事情:
hbs.registerPartial('headPartial', 'header');
// where "header" is an .hbs file in my views folder
Run Code Online (Sandbox Code Playgroud)
但是,它正在抛出"标题部分无法找到".
如果我将一个html字符串传递给第二个参数,我可以使registerPartial工作,但我想为我的部分使用单独的视图文件.
我没有找到任何关于此的文档,但希望我可能只是遗漏了一些简单的东西.
有谁知道如何在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);
Run Code Online (Sandbox Code Playgroud)
这是我的routes.index文件:
exports.index = function(req, res){
res.render('index', { title: 'Express' })
};
Run Code Online (Sandbox Code Playgroud)
在我的views文件夹中,我有三个模板:
views/
header.hbs (this is my partial)
index.hbs
layout.hbs
Run Code Online (Sandbox Code Playgroud)
在我的index.hbs文件中,我用以下方法调用'headPartial'部分:
{{> headPartial}}
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏.
Ben*_*son 42
此代码加载目录中的所有部分模板,并通过filename使其可用:
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);
});
Run Code Online (Sandbox Code Playgroud)
mil*_*ord 40
为方便起见,registerPartials提供了一种从特定目录加载所有部分的快速方法:
var hbs = require('hbs');
hbs.registerPartials(__dirname + '/views/partials');
Run Code Online (Sandbox Code Playgroud)
从目录加载的部分是根据文件名命名的,其中空格和连字符用下划线字符替换:
template.html -> {{> template}}
template 2.html -> {{> template_2}}
login view.hbs -> {{> login_view}}
template-file.html -> {{> template_file}}
Run Code Online (Sandbox Code Playgroud)
干杯!
swa*_*ins 12
看起来像创建变量并手动拉入模板代码有效:
var hbs = require('hbs')
, fs = require('fs')
, headerTemplate = fs.readFileSync(__dirname + '/views/_header.hbs', 'utf8');
Run Code Online (Sandbox Code Playgroud)
然后:
hbs.registerPartial('headPartial', headerTemplate);
Run Code Online (Sandbox Code Playgroud)
对我来说,我有模板文件 my-partial.hbs
然后我尝试通过以下方式访问它们:
{{> my-partial }}
Run Code Online (Sandbox Code Playgroud)
但是无论文件名如何,部分都作为 my_partial 存储在 hbs 中。
这要归功于 hbs version 3.1.0 line 218
.slice(0, -(ext.length)).replace(/[ -]/g, '_').replace('\\', '/');
Run Code Online (Sandbox Code Playgroud)
这是在自述文件中
| 归档时间: |
|
| 查看次数: |
31604 次 |
| 最近记录: |