【问题标题】:How to send a variable from express to pug?如何将变量从快递发送到哈巴狗?
【发布时间】:2019-08-10 08:54:21
【问题描述】:

我想将一个变量从 Express 发送到 Pug。

因此我已经发现我必须使用 res.render(),但是如果我尝试这个,我只会得到一个未定义的变量。

在我的 index.js 中:

app.post("/example", upload.single("photo"), (req, res) => { 
  {... some code}
  var html = "some text";
  res.render("results", {
    html: html
  });

在我的 results.pug 中,我尝试将文本显示为字符串:

p "#{html}"

目前我的页面上只有两个“”。 但我需要显示结果

感谢您的帮助!!!! :)

【问题讨论】:

  • 您是否设置了渲染引擎(正确)?类似app.set('view engine', 'pug')
  • 我已经向 pug 发送了以下信息:const resultsTemplate = pug.compileFile("public/results.pug") 并发送了一些信息:res.send(resultsTemplate({ image: base64String })),但这不适用于变量...

标签: javascript express pug


【解决方案1】:

您的问题在于如何在 pug 模板中调用变量。这是最简单的方法:

p= html

p
  div= html

如果你真的想使用interpolation,语法应该是:

p #{html}

我敢打赌,如果您在当前页面上查看源代码,您将看到如下所示的内容:

<p>#{html}</p>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 2017-01-23
    • 2021-07-23
    • 1970-01-01
    • 2018-05-17
    • 2018-09-15
    相关资源
    最近更新 更多