3

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

Fred Mériot
  • 4,157
  • 9
  • 33
  • 50

1 Answers1

1

You can pass a second parameter to your partial:

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

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

Community
  • 1
  • 1
brianc
  • 1,547
  • 3
  • 16
  • 30