How to set a variable from the Nunjucks extension? For example, here is a template and extension. The variable xshould be visible only inside the block sample.
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) {
var ret = new nunjucks.runtime.SafeString(body());
return ret;
};
}
source
share