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

Categories

0 votes
694 views
in Technique[技术] by (71.8m points)

node.js - Node JS Pass a Variable to Jade / Pug

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

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

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...