I have an Express app using Pug templates and Parcel for my bundler. According to the Parcel docs, I can have a config file that stores my locals pug.config.js
, however, I need to dynamically add locals to my templates at runtime. Is it possible to do this?
Here are my files:
index.pug
...
h1 #{isAuthenticated}
h1 #{env}
...
pug.config.js
module.exports = {
locals: {
env: process.env.NODE_ENV
}
}
app.js
const Bundler = require('parcel-bundler')
const bundler = new Bundler(path.resolve(__dirname, 'views/index.pug'))
app
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'pug')
.use(function(req, res, next) {
res.locals.isAuthenticated = 'true'
next()
})
.use(bundler.middleware())
Here I am attempting to set a locals var isAuthenticated
to 'true'
, however when rendering index.pug
the variable is empty, meanwhile, the env
var from my pug.config.js
file is present.
Now, if I try to render a page in my controller functions with res.render('index', {isAuthenticated: 'true'})
the variable isAuthenticated
is now present, however env
is empty.
I'm wondering if I am missing something here or if this works as expected?