1
-- 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

dpak005
  • 241
  • 4
  • 10

3 Answers3

0

Take the return out of the last line. Also its called pug now not jade.

Jason Livesay
  • 6,317
  • 3
  • 25
  • 31
0

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'
  });
});
abdulbarik
  • 6,101
  • 5
  • 38
  • 59
  • Nope, tried that.it does not get pass into separate header.jade but is present in the jade file which is extending the layout.jade. – dpak005 Oct 09 '16 at 05:52
  • As you said you are trying to use common header with title then you need to save your title value somewhere in client side since your are rendering `index` page not `header.pug`. – abdulbarik Oct 09 '16 at 06:13
  • its nor the value of title that needs to be set, i need to set the value of userName which is present in header.pug, and header.pug is included in layout.pug, AND index.pug extends layout.pug. i hope u understand what i am trying to do. – dpak005 Oct 09 '16 at 06:22
  • Ohh sorry!! it's my mismatch but you can get that value if you render that page or just save somewhere and use it – abdulbarik Oct 09 '16 at 06:24
  • tried that.. in the end i wrote the content of header.pug into layout.pug itself – dpak005 Oct 10 '16 at 04:55
0

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 .

miguelmorin
  • 5,025
  • 4
  • 29
  • 64