Node JS 将变量传递给 Jade/Pug [英] Node JS Pass a Variable to Jade / Pug

查看:70
本文介绍了Node JS 将变量传递给 Jade/Pug的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我无法使用 Node JS 将变量传递给 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

这只是在 index.html 文件中返回guy"

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

推荐答案

我认为您正在使用 JADE 编码 (#{hello}) 和带有静态 .html 的pug"(更新的 jade)插件——完全错误.

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

按照以下几行:

  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. 然后在模板文件index.pug"中回显;在/public/views"中

html
  head
  title= title
body
  h1= message

这篇关于Node JS 将变量传递给 Jade/Pug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆