How can I define global variables in nunjucks?

前端 未结 3 1431
独厮守ぢ
独厮守ぢ 2021-01-12 07:30

Using nunjucks, how can I define some global variables that should always be available within all templates?

Ideally, they would be specified somewhere in the enviro

3条回答
  •  隐瞒了意图╮
    2021-01-12 08:34

    It's not documented (or perhaps advised), but this works:

    var njglobals = require('nunjucks/src/globals');
    njglobals.someVar = 'someValue';
    

    You can now use someVar in your templates.

    Be sure not to overwrite any of the existing properties of the njglobals object, though (for nunjucks@1.0.1, they are range, cycler and joiner).

提交回复
热议问题