Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

For some reason I can't pass a variable to the pug template with Node JS.

app.get("/", function (req, res) {
    res.render('index', { hello : 'Hey'} )
})

....

extends layout.pug

block content
    h1 #{hello} guy

This just returns "guy" in the index.html file

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
692 views
Welcome To Ask or Share your Answers For Others

1 Answer

I think you are using JADE coding (#{hello}) with "pug"(updated jade) plugin with static .html -- completely wrong.

Follow the lines below:

  1. Use this first
app.set('views', __dirname + '/public/views');
app.set('view engine', 'pug');
  1. Then pass this to first visit
app.get('/', function (req, res) {
   res.render('index', { title: 'Hey', message: 'Hello there!'});
});
  1. Then echo in template file "index.pug" in "/public/views"
html
  head
  title= title
body
  h1= message

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...