Created
January 12, 2015 18:40
-
-
Save SamyPesse/409cd5777825099ed445 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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