node.js - Node JS 将变量传递给 Jade/Pug

标签 node.js pug

由于某种原因,我无法使用 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}) -- 完全错误。

按照以下几行:

  1. 先用这个
app.set('views', __dirname + '/public/views');
app.set('view engine', 'pug');
  1. 然后将此传递给首次访问
app.get('/', function (req, res) {
   res.render('index', { title: 'Hey', message: 'Hello there!'});
});
  1. 然后在“/public/views”中的模板文件“index.pug”中回显
html
  head
  title= title
body
  h1= message

关于node.js - Node JS 将变量传递给 Jade/Pug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38044237/

相关文章:

javascript - 为用户提供编辑 Marko 模板的选项是否安全?

javascript - inquirer.js - 将 BottomBar 与顺序日志相结合

javascript - 如何将脚本加载到 webpack 中

javascript - nodejs, Jade 转义标记

javascript - 如何在客户端的jadecompile()中使用 "locals"选项?

javascript - 使用 JavaScript 调用 Node.js 后端函数的最有效方法是什么

node.js - 无法使用 CF 定位组织

css - VueJS 转换与 Pug partials

javascript - 如何选择 div 的每个 child 并获得第 n 个数字

html - : express + jade/ejs + html5 + css + websockets 的逻辑是什么