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\'}
I think you are using JADE coding (#{hello}) with "pug"(updated jade) plugin with static .html -- completely wrong.
follow the lines below:
use this first
app.set('views', __dirname + '/public/views'); app.set('view engine', 'pug');
thEn pass this to first visit
app.get('/', function (req, res) { res.render('index', { title: 'Hey', message: 'Hello there!'}); });
thEn echo in template file "index.pug" in "/public/views"
html head title= title body h1= message
Try this.. works for me.
nodejs part / 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 part / (index.pug)
script.
var testObj = !{testObj};
i'm using pug version: 2.0.3