【问题标题】:nodejs + HBS (handlebars) : passing data to partialsnodejs + HBS(车把):将数据传递给部分
【发布时间】:2013-11-17 08:30:43
【问题描述】:

我有一个关于 HBS 和 partials 的小问题:如何将数据传递给 partial? 假设我有一个 index.html 模板,其中包含一个 head.html 部分。

我有这样的代码:

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>

所以,当我调用我的 localhost:8090/test 时,一切正常,除了部分不显示 {{head}} 数据。

如何将数据传递给我的部分?

感谢您的帮助

【问题讨论】:

    标签: node.js handlebars.js partials


    【解决方案1】:

    您可以将第二个参数传递给您的部分:

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

    看到这个答案:https://stackoverflow.com/a/11615370/208022

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 2021-09-02
      • 2014-06-24
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多