从 pug 中读取环境变量 [英] Reading environment variables from pug

查看:76
本文介绍了从 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.

这篇关于从 pug 中读取环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆