Skip to content

Commit

Permalink
Merge pull request #990 from noahlange/914-macro-set-block
Browse files Browse the repository at this point in the history
Addresses 914; capture blocks in macros now write to temporary output buffer
  • Loading branch information
fdintino authored May 21, 2017
2 parents be0cfec + 04b0fb5 commit 5312e66
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1059,13 +1059,19 @@ var Compiler = Object.extend({
},

compileCapture: function(node, frame) {
// we need to temporarily override the current buffer id as 'output'
// so the set block writes to the capture output instead of the buffer
var buffer = this.buffer;
this.buffer = 'output';
this.emitLine('(function() {');
this.emitLine('var output = "";');
this.withScopedSyntax(function () {
this.compile(node.body, frame);
});
this.emitLine('return output;');
this.emitLine('})()');
// and of course, revert back to the old buffer id
this.buffer = buffer;
},

compileOutput: function(node, frame) {
Expand Down
12 changes: 12 additions & 0 deletions tests/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,18 @@
''
);

/**
* Capture blocks inside macros were printing to the main buffer instead of
* the temporary one, see https://github.com/mozilla/nunjucks/issues/914.
**/
equal('{%- macro foo(bar) -%}'+
'{%- set test -%}foo{%- endset -%}'+
'{{ bar }}{{ test }}'+
'{%- endmacro -%}'+
'{{ foo("bar") }}',
'barfoo'
);

equal('{% set block_content %}test string{% endset %}'+
'{{ block_content }}',
'test string'
Expand Down

0 comments on commit 5312e66

Please sign in to comment.