出于某种原因,我无法使用Node JS将变量传递给pug模板.
app.get("/", function (req, res) {
res.render('index', { hello : 'Hey'} )
})
Run Code Online (Sandbox Code Playgroud)
....
extends layout.pug
block content
h1 #{hello} guy
Run Code Online (Sandbox Code Playgroud)
这只是在index.html文件中返回"guy"
See*_*ngh 15
我认为你使用JADE编码(#{hello})和带有静态.html的"pug"(更新的jade)插件 - 完全错误.
请按照以下几行:
首先使用它
app.set('views', __dirname + '/public/views');
app.set('view engine', 'pug');
Run Code Online (Sandbox Code Playgroud)而不是通过这个第一次访问
app.get('/', function (req, res) {
res.render('index', { title: 'Hey', message: 'Hello there!'});
});
Run Code Online (Sandbox Code Playgroud)比"/ public/views"中的模板文件"index.pug"中的echo更
html
head
title= title
body
h1= message
Run Code Online (Sandbox Code Playgroud)小智 6
试试这个..为我工作。
Node.js零件/ index.js
const router = require('express').Router();
router.get('/', (req, res, next) => {
const testObj = {hello: 'Hey'};
res.render('index', { testObj: JSON.stringify(testObj) });
});
Run Code Online (Sandbox Code Playgroud)
哈巴狗/玉器零件/(index.pug)
script.
var testObj = !{testObj};
Run Code Online (Sandbox Code Playgroud)
我正在使用哈巴狗版本:2.0.3