繁体   English   中英

如何使用 node-express 在包含的 pug 模板中传递数据

[英]How to pass data in included pug template using node-express

-- header.jade
ul
    li
        a User = #{userName}

-- layout.jade
doctype html
html
  head
    title= title
  body
    include header

    include index

-- route file: index.js
router.get('/', function(req, res, next) {
  res.render('index', {
            userName: 'Jane Doe'
        });
});

如何获取 layout.jade 文件中包含的 header jade 模板中的 userName 值。 我正在尝试为我的项目实现一个通用的标题概念。

任何帮助将不胜感激! TIA

从最后一行中取出return 现在它也叫pug而不是jade

您以错误的方式返回您的回复。

使用这样的所需模板返回您的响应数据

 router.get('/', function(req, res, next) {
 return res.render('index', {
    userName: 'Jane Doe'
  });
});

你想要的行为现在是默认值,变量被传递到包含的视图。 如果需要,还可以使用混合函数,请参阅Jade include with parameter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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