index.js 690 B

123456789101112131415161718192021222324
  1. var vm = require('vm');
  2. module.exports = function(nunjucks) {
  3. return function DoExtension(_env) {
  4. this.tags = ['do'];
  5. this.parse = function(parser, nodes, lexer) {
  6. var tok = parser.nextToken();
  7. var args = parser.parseSignature(null, true);
  8. parser.advanceAfterBlockEnd(tok.value);
  9. var body = parser.parseUntilBlocks('enddo');
  10. parser.advanceAfterBlockEnd();
  11. return new nodes.CallExtension(this, 'run', args, [body]);
  12. };
  13. this.run = function(context, body) {
  14. var js = body();
  15. vm.runInNewContext(js, context.ctx);
  16. return '';
  17. };
  18. };
  19. };