Skip to content

Mustachio: update 'current' template when rendering a partial #2497

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

Merged
merged 1 commit into from
Jan 29, 2021
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
16 changes: 13 additions & 3 deletions lib/src/mustachio/renderer_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,18 @@ abstract class RendererBase<T> {
/// The renderer of the parent context, if any, otherwise `null`.
final RendererBase parent;

final Template template;
/// The current template being rendered.
///
/// When rendering a partial, the [context] object, and hence the
/// [RendererBase] does not change, only this current template.
Template _template;

/// The output buffer into which [context] is rendered, using a template.
final buffer = StringBuffer();

RendererBase(this.context, this.parent, this.template);
RendererBase(this.context, this.parent, this._template);

Template get template => _template;

void write(String text) => buffer.write(text);

Expand Down Expand Up @@ -245,7 +251,11 @@ abstract class RendererBase<T> {
void partial(Partial node) {
var key = node.key;
var partialFile = template.partials[key];
renderBlock(template.partialTemplates[partialFile].ast);
var partialTemplate = template.partialTemplates[partialFile];
var outerTemplate = _template;
_template = partialTemplate;
renderBlock(partialTemplate.ast);
_template = outerTemplate;
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/mustachio/renderer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,20 @@ void main() {
expect(renderBar(bar, barTemplate), equals('Text Partial goodbye'));
});

test('Renderer renders a partial which refers to other partials', () async {
var barTemplateFile = getFile('/project/bar.mustache')
..writeAsStringSync('Text, {{#foo}}{{>foo.mustache}}{{/foo}}');
getFile('/project/foo.mustache')
.writeAsStringSync('p1, {{#l1}}{{>foo_l1.mustache}}{{/l1}}');
getFile('/project/foo_l1.mustache').writeAsStringSync('p2 {{.}}, ');
var barTemplate = await Template.parse(barTemplateFile);
var bar = Bar()
..foo = (Foo()
..s1 = 'hello'
..l1 = [1, 2, 3]);
expect(renderBar(bar, barTemplate), equals('Text, p1, p2 1, p2 2, p2 3, '));
});

test('Renderer renders a partial with a heterogeneous context chain',
() async {
var barTemplateFile = getFile('/project/bar.mustache')
Expand Down