Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addresses 914; capture blocks in macros now write to temporary output buffer #990

Merged
merged 1 commit into from
May 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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