简体   繁体   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'
        });
});

how to get the userName value in header jade template which is included in the layout.jade file.如何获取 layout.jade 文件中包含的 header jade 模板中的 userName 值。 i am trying to implement a common header concept for my project.我正在尝试为我的项目实现一个通用的标题概念。

Any help would be highly appreciated!任何帮助将不胜感激! TIA TIA

Take the return out of the last line.从最后一行中取出return Also its called pug now not jade .现在它也叫pug而不是jade

You are returning your response in wrong way.您以错误的方式返回您的回复。

Return your response data with needed template like this使用这样的所需模板返回您的响应数据

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

The behavior you want is now the default and variables get passed to included views.你想要的行为现在是默认值,变量被传递到包含的视图。 If you want, you can also use mix-in functions, see Jade include with parameter .如果需要,还可以使用混合函数,请参阅Jade include with parameter

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

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