Skip to content

Commit 174a198

Browse files
authored
Mustachio: update 'current' template when rendering a template (#2497)
1 parent 30b4df2 commit 174a198

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lib/src/mustachio/renderer_base.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,18 @@ abstract class RendererBase<T> {
136136
/// The renderer of the parent context, if any, otherwise `null`.
137137
final RendererBase parent;
138138

139-
final Template template;
139+
/// The current template being rendered.
140+
///
141+
/// When rendering a partial, the [context] object, and hence the
142+
/// [RendererBase] does not change, only this current template.
143+
Template _template;
140144

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

144-
RendererBase(this.context, this.parent, this.template);
148+
RendererBase(this.context, this.parent, this._template);
149+
150+
Template get template => _template;
145151

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

@@ -245,7 +251,11 @@ abstract class RendererBase<T> {
245251
void partial(Partial node) {
246252
var key = node.key;
247253
var partialFile = template.partials[key];
248-
renderBlock(template.partialTemplates[partialFile].ast);
254+
var partialTemplate = template.partialTemplates[partialFile];
255+
var outerTemplate = _template;
256+
_template = partialTemplate;
257+
renderBlock(partialTemplate.ast);
258+
_template = outerTemplate;
249259
}
250260
}
251261

test/mustachio/renderer_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,20 @@ void main() {
336336
expect(renderBar(bar, barTemplate), equals('Text Partial goodbye'));
337337
});
338338

339+
test('Renderer renders a partial which refers to other partials', () async {
340+
var barTemplateFile = getFile('/project/bar.mustache')
341+
..writeAsStringSync('Text, {{#foo}}{{>foo.mustache}}{{/foo}}');
342+
getFile('/project/foo.mustache')
343+
.writeAsStringSync('p1, {{#l1}}{{>foo_l1.mustache}}{{/l1}}');
344+
getFile('/project/foo_l1.mustache').writeAsStringSync('p2 {{.}}, ');
345+
var barTemplate = await Template.parse(barTemplateFile);
346+
var bar = Bar()
347+
..foo = (Foo()
348+
..s1 = 'hello'
349+
..l1 = [1, 2, 3]);
350+
expect(renderBar(bar, barTemplate), equals('Text, p1, p2 1, p2 2, p2 3, '));
351+
});
352+
339353
test('Renderer renders a partial with a heterogeneous context chain',
340354
() async {
341355
var barTemplateFile = getFile('/project/bar.mustache')

0 commit comments

Comments
 (0)