如何从Nunjucks扩展中设置变量?例如,这是一个模板和一个扩展名。 x
变量应仅在sample
块中可见。
模板:
{% sample %}
{{ x }}
{% endsample %}
扩展:
function SampleExtension() {
this.tags = ['sample'];
this.parse = function(parser, nodes, lexer) {
var tok = parser.nextToken();
var args = parser.parseSignature(null, true);
parser.advanceAfterBlockEnd(tok.value);
var body = parser.parseUntilBlocks('endsample');
parser.advanceAfterBlockEnd();
return new nodes.CallExtension(this, 'run', args, [ body ]);
};
this.run = function(context, args, body) {
// I'm guessing I need to mess with the context variable here?
var ret = new nunjucks.runtime.SafeString(body());
return ret;
};
}