How can I define global variables in nunjucks?

前端 未结 3 1459
独厮守ぢ
独厮守ぢ 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:23

    It might be also helpfull for someone. It is possible to avoid writing any js code when dealing with global variables in nunjucks.

    You need to create a _globals.html file, which contains all the global variables.

    {% set some_var1 = "Foo" %}
    {% set some_var2 = "Bar" %}
    

    Then include _globals.html to any page, where you need the global variable. E.g. somePage.html

    {% import '_globals.html' as globals %}
    
    {{globals.some_var1 }}
    {{globals.some_var2 }}
    

    For more info please check http://mozilla.github.io/nunjucks/templating.html#set

提交回复
热议问题