Skip to content

Commit

Permalink
Fix include tag expression cache
Browse files Browse the repository at this point in the history
Fixes #343
  • Loading branch information
suggyd authored May 20, 2021
1 parent ee79e4a commit 4ade8d9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
45 changes: 45 additions & 0 deletions Fluid.Tests/IncludeStatementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
13 changes: 7 additions & 6 deletions Fluid/Ast/IncludeStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ public override async ValueTask<Completion> 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);
Expand Down

0 comments on commit 4ade8d9

Please sign in to comment.