How can I set a variable from a Nunjucks extension? For example, here is a template and an extension. The x
variable should only be visible from within the sample
block.
Template:
{% sample %}
{{ x }}
{% endsample %}
Extension:
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;
};
}