Skip to content

Commit 099cb04

Browse files
xoofxCopilot
andcommitted
Clear include cache on TemplateContext.Reset
Co-authored-by: Copilot <[email protected]>
1 parent f55280a commit 099cb04

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/Scriban.Tests/TestIncludes.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,48 @@ public ValueTask<string> LoadAsync(TemplateContext context, SourceSpan callerSpa
7373
}
7474
}
7575

76+
private sealed class SwitchingLoader : ITemplateLoader
77+
{
78+
public string Content { get; set; } = string.Empty;
79+
80+
public string GetPath(TemplateContext context, SourceSpan callerSpan, string templateName)
81+
{
82+
return templateName;
83+
}
84+
85+
public string Load(TemplateContext context, SourceSpan callerSpan, string templatePath)
86+
{
87+
return Content;
88+
}
89+
90+
public ValueTask<string> LoadAsync(TemplateContext context, SourceSpan callerSpan, string templatePath)
91+
{
92+
return ValueTask.FromResult(Load(context, callerSpan, templatePath));
93+
}
94+
}
95+
96+
[Test]
97+
public void ResetShouldClearCachedTemplates()
98+
{
99+
var loader = new SwitchingLoader { Content = "admin-only" };
100+
var context = new TemplateContext
101+
{
102+
TemplateLoader = loader
103+
};
104+
var template = Template.Parse("{{ include 'profile' }}");
105+
106+
var first = template.Render(context);
107+
Assert.AreEqual("admin-only", first);
108+
Assert.That(context.CachedTemplates, Does.ContainKey("profile"));
109+
110+
context.Reset();
111+
loader.Content = "guest-view";
112+
113+
var second = template.Render(context);
114+
Assert.AreEqual("guest-view", second);
115+
Assert.That(context.CachedTemplates, Does.ContainKey("profile"));
116+
}
117+
76118
[Test]
77119
public void TestIndentedNestedIncludes()
78120
{
@@ -557,4 +599,4 @@ public void TestIncludeJoinWithTemplateDelimiters()
557599
Assert.AreEqual(expectedString, template.Render(context));
558600
}
559601
}
560-
}
602+
}

src/Scriban/TemplateContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,8 @@ public virtual void Reset()
899899
{
900900
PopSourceFile();
901901
}
902+
903+
CachedTemplates.Clear();
902904
}
903905

904906
/// <summary>
@@ -1387,4 +1389,4 @@ public override string GetTemplatePathFromName(string templateName, ScriptNode c
13871389
return ConvertTemplateNameToPath(templateName, callerContext);
13881390
}
13891391
}
1390-
}
1392+
}

0 commit comments

Comments
 (0)