由于某种原因,我无法使用 Node JS 将变量传递给 pug 模板。
app.get("/", function (req, res) {
res.render('index', { hello : 'Hey'} )
})
....
extends layout.pug
block content
h1 #{hello} guy
这只是在 index.html 文件中返回“guy”
最佳答案
我认为您正在使用带有静态 .html 的“pug”(更新的 Jade )插件的 JADE 编码 (#{hello}) -- 完全错误。
按照以下几行:
- 先用这个
app.set('views', __dirname + '/public/views');
app.set('view engine', 'pug');
- 然后将此传递给首次访问
app.get('/', function (req, res) {
res.render('index', { title: 'Hey', message: 'Hello there!'});
});
- 然后在“/public/views”中的模板文件“index.pug”中回显
html
head
title= title
body
h1= message
关于node.js - Node JS 将变量传递给 Jade/Pug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38044237/