Reading environment variables from pug

孤者浪人 提交于 2019-12-13 17:16:14

问题


I'm using pug to compile static html. My own static site generator, kinda.

I have no node.js server code besides this line in my package.json file: "watch-pages": "pug -O options.json -w pages/ --out _static-website/"

But, I need to read environment variables like NODE_ENV inside of pug templates. How might I do this?


回答1:


This is fairly simple; you may find another way to do it but what I tried (successfully) was to simply define a .js file to pass as the options parameter which includes the variables I wanted. For example:

// env.js
module.exports = { env: process.env };

Then the template can be something like:

// tmp.pug
ul
  each e in env
    li=e

And you can then run pug -O env.js tmp.html and it will create a env.html with the environment variables rendered as list items.



来源:https://stackoverflow.com/questions/44549022/reading-environment-variables-from-pug

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