Skip to content

Instantly share code, notes, and snippets.

@SamyPesse
Created January 12, 2015 18:40
Show Gist options
  • Save SamyPesse/409cd5777825099ed445 to your computer and use it in GitHub Desktop.
Save SamyPesse/409cd5777825099ed445 to your computer and use it in GitHub Desktop.
function AutoEscapeExtension(_env) {
this.tags = ['autoescape'];
this.parse = function(parser, nodes, lexer) {
// get the tag token
var tok = parser.nextToken();
// parse the args and move after the block end. passing true
// as the second arg is required if there are no parentheses
var args = parser.parseSignature(null, true);
parser.advanceAfterBlockEnd(tok.value);
// parse the body and possibly the error block, which is optional
var body = parser.parseUntilBlocks('endautoescape');
parser.advanceAfterBlockEnd();
// See above for notes about CallExtension
return new nodes.CallExtension(this, 'run', args, [body]);
};
this.run = function(context, mode, body) {
var ret, before = _env.autoesc;
_env.autoesc = !!mode;
ret = new nunjucks.runtime.SafeString(body());
_env.autoesc = before;
return ret;
};
}
env.addExtension('AutoEscapeExtension', new AutoEscapeExtension(env));
{% autoescape false %}
This is a {{ test }}
{% endautoescape %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment