从 pug 中读取环境变量
我正在使用 pug 来编译静态 html.我自己的静态网站生成器,有点.
I'm using pug to compile static html. My own static site generator, kinda.
我的 package.json 文件中除了这一行之外没有 node.js 服务器代码: "watch-pages": "pug -O options.json -w pages/--out _static-website/"代码>
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/"
但是,我需要在 pug 模板中读取像 NODE_ENV 这样的环境变量.我该怎么做?
But, I need to read environment variables like NODE_ENV inside of pug templates. How might I do this?
这相当简单;你可能会找到另一种方法来做到这一点,但我尝试(成功)是简单地定义一个 .js 文件作为包含我想要的变量的选项参数传递.例如:
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
然后您可以运行 pug -O env.js tmp.html
它将创建一个 env.html,其中环境变量呈现为列表项.
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.