Node.js 如何在pug渲染中访问全局对象

Node.js 如何在pug渲染中访问全局对象,node.js,pug,Node.js,Pug,我正在使用fastify with point of view模块渲染pug模板: Fastify.register(require('point-of-view'), { engine: { pug: require('pug') }, templates: Path.join(__dirname,'templates'), options: { filename: Path.join(__dirname,'templates/layou

我正在使用fastify with point of view模块渲染pug模板:

Fastify.register(require('point-of-view'), {
    engine: {
      pug: require('pug')
    },
    templates: Path.join(__dirname,'templates'),
    options: {
      filename: Path.join(__dirname,'templates/layout.pug'),
      globals: [
        {
          assets_path: 'Path_to_assets'
        }
      ]
    }
  })

如何访问pug模板中的全局变量?

我找到了一种方法:访问环境变量

在我的模板中,我使用

global.process.env.ASSETS_PATH

如果资产路径声明为我的环境变量的一部分,则不能访问“选项”参数。但您可以通过defaultContext传递变量,如下所示:

fastify.register(require('point-of-view'), {
  engine: {
    pug: require('pug')
  },


  defaultContext: {
    // Place your variable here
    globals: [],
    myVars,
    appName: 'Fastify Website'
  },


  templates: Path.join(__dirname,'templates'),
  options: {
    filename: Path.join(__dirname,'templates/layout.pug'),
    globals: [
      {
        assets_path: 'Path_to_assets'
      }
    ]
  }
})