简体   繁体   中英

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. i am trying to implement a common header concept for my project.

Any help would be highly appreciated! TIA

Take the return out of the last line. Also its called pug now not 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 .

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