Node JS Pass a Variable to Jade / Pug

元气小坏坏 提交于 2019-11-30 11:44:09
Seetpal singh

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

follow the lines below:

  1. use this first

    app.set('views', __dirname + '/public/views');
    app.set('view engine', 'pug');
    
  2. than pass this to first visit

    app.get('/', function (req, res) {
        res.render('index', { title: 'Hey', message: 'Hello there!'});
    });
    
  3. than 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!