简体   繁体   中英

nodejs + HBS (handlebars) : passing data to partials

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

You can pass a second parameter to your partial:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM