From 4ade8d999b482e263ed981b17af6487580b0faae Mon Sep 17 00:00:00 2001 From: suggyd Date: Thu, 20 May 2021 15:40:45 +0100 Subject: [PATCH] Fix include tag expression cache Fixes #343 --- Fluid.Tests/IncludeStatementTests.cs | 45 ++++++++++++++++++++++++++++ Fluid/Ast/IncludeStatement.cs | 13 ++++---- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/Fluid.Tests/IncludeStatementTests.cs b/Fluid.Tests/IncludeStatementTests.cs index c17914a6..a673890e 100644 --- a/Fluid.Tests/IncludeStatementTests.cs +++ b/Fluid.Tests/IncludeStatementTests.cs @@ -62,6 +62,51 @@ public async Task IncludeSatement_ShouldLoadPartial_IfThePartialsFolderExist() Assert.Equal(expectedResult, sw.ToString()); } + [Fact] + public async Task IncludeSatement_ShouldLoadCorrectTemplate_IfTheMemberExpressionValueChanges() + { + var expression = new MemberExpression(new IdentifierSegment("Firstname")); + var sw = new StringWriter(); + + var fileProvider = new MockFileProvider(); + fileProvider.Add("_First.liquid", @"{{ 'Partial Content One' }} +Partials_One: '{{ Partials }}' +color_One: '{{ color }}' +shape_One: '{{ shape }}'"); + + fileProvider.Add("_Second.liquid", @"{{ 'Partial Content Two' }} +Partials_Two: '{{ Partials }}' +color_Two: '{{ color }}' +shape_Two: '{{ shape }}'"); + + var model = new Domain.Person { Firstname = "_First.liquid" }; + + var options = new TemplateOptions() { FileProvider = fileProvider }; + var context = new TemplateContext(model, options); + var expectedResultFirstCall = @"Partial Content One +Partials_One: '' +color_One: '' +shape_One: ''"; + + var expectedResultSecondCall = @"Partial Content Two +Partials_Two: '' +color_Two: '' +shape_Two: ''"; + + var include = new IncludeStatement(_parser, expression); + + await include.WriteToAsync(sw, HtmlEncoder.Default, context); + + Assert.Equal(expectedResultFirstCall, sw.ToString()); + + model.Firstname = "_Second.liquid"; + sw = new StringWriter(); + + await include.WriteToAsync(sw, HtmlEncoder.Default, context); + + Assert.Equal(expectedResultSecondCall, sw.ToString()); + } + [Fact] public async Task IncludeSatement_WithInlinevariableAssignment_ShouldBeEvaluated() { diff --git a/Fluid/Ast/IncludeStatement.cs b/Fluid/Ast/IncludeStatement.cs index 40377a4c..5b27861c 100644 --- a/Fluid/Ast/IncludeStatement.cs +++ b/Fluid/Ast/IncludeStatement.cs @@ -31,14 +31,15 @@ public override async ValueTask WriteToAsync(TextWriter writer, Text { context.IncrementSteps(); - if (_template == null) + var relativePath = (await Path.EvaluateAsync(context)).ToStringValue(); + + if (!relativePath.EndsWith(ViewExtension, StringComparison.OrdinalIgnoreCase)) { - var relativePath = (await Path.EvaluateAsync(context)).ToStringValue(); - if (!relativePath.EndsWith(ViewExtension, StringComparison.OrdinalIgnoreCase)) - { - relativePath += ViewExtension; - } + relativePath += ViewExtension; + } + if (_template == null || !string.Equals(_identifier, System.IO.Path.GetFileNameWithoutExtension(relativePath), StringComparison.OrdinalIgnoreCase)) + { var fileProvider = context.Options.FileProvider; var fileInfo = fileProvider.GetFileInfo(relativePath);