nodejs + HBS (handlebars) : passing data to partials

六眼飞鱼酱① 提交于 2020-02-01 06:18:22

问题


I have a little question about HBS and partials : How can I pass data to a partial ? Suppose I have an index.html template which include a head.html partial.

I have a code like this :

server.js :

var express = require('express');
var app = express();
var hbs = require('hbs');

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

app.set('view engine', 'html');
app.engine('html', hbs.__express);
app.use(express.bodyParser());

app.get('/:item', function(req, res) {
   res.render('index',{title:"My Blog", item : req.param("item"), head : "hello world!"});
});

app.listen(8090);

index.html :

{{> head}}
Title : {{title}} - {{item}}

head.html :

<h1>{{head}} - HEAD</h1>

So, when I call my localhost:8090/test, everything works fine except the fact that the partial does not display the {{head}} data.

How can I pass data to my partial?

Thanks for your help


回答1:


You can pass a second parameter to your partial:

{{> head this}}
Title : {{title}} - {{item}}

See this answer : https://stackoverflow.com/a/11615370/208022



来源:https://stackoverflow.com/questions/19806399/nodejs-hbs-handlebars-passing-data-to-partials

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!