如何在Nunjucks中查找当前上下文?

cal*_*lum 1 nunjucks

在Handlebars中,您可以使用this查找当前上下文.

你怎么在Nunjucks做同样的事情?

例如,如果要将整个上下文转储为JSON字符串:

<script>window.__config__ = {{ this | dump | safe }};</script>
Run Code Online (Sandbox Code Playgroud)

(但this似乎在Nunjucks中没有用.)

Aik*_*wai 5

如果需要context,可以添加全局功能

var env = nunjucks.configure([...
...
env.addGlobal('getContext', function() { 
    return this.ctx;
})
Run Code Online (Sandbox Code Playgroud)

并将她的结果转储到模板中

{{ getContext() | dump| safe }}
Run Code Online (Sandbox Code Playgroud)