由于某些原因,我无法通过Node JS将变量传递到pug模板。
app.get("/", function (req, res) {
res.render('index', { hello : 'Hey'} )
})
....
extends layout.pug
block content
h1 #{hello} guy
这只是在index.html文件中返回"guy"
我认为你正在使用JADE代码 (#{hello}) 与 "pug" (更新版的jade) 插件和静态 .html 文件-- 这是完全错误的。
按照以下步骤进行:
app.set('views', __dirname + '/public/views');
app.set('view engine', 'pug');
app.get('/', function (req, res) {
res.render('index', { title: 'Hey', message: 'Hello there!'});
});
html
head
title= title
body
h1= message
试一下这个... 对我有用。
nodejs 部分 / index.js
const router = require('express').Router();
router.get('/', (req, res, next) => {
const testObj = {hello: 'Hey'};
res.render('index', { testObj: JSON.stringify(testObj) });
});
pug/jade部分 / (index.pug)
script.
var testObj = !{testObj};
我正在使用 pug 版本:2.0.3
app
是什么?layout.pug
有什么内容? - chharvey