Skip to content

Commit

Permalink
Define fallback scope in TemplateOptions (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros authored Feb 10, 2021
1 parent 0a007c6 commit e1555e3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
24 changes: 23 additions & 1 deletion Fluid.Tests/TemplateContextTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Fluid.Values;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -28,5 +29,26 @@ private static async Task Register()
var templateContext = new TemplateContext();
templateContext.Options.MemberAccessStrategy.Register(typeof(TestClass));
}


[Fact]
public void ScopeShouldFallbackToTemplateOptions()
{
var parser = new FluidParser();

parser.TryParse("{{ p.NaMe }}", out var template, out var error);

var options = new TemplateOptions();
options.Scope.SetValue("o1", new StringValue("o1"));
options.Scope.SetValue("o2", new StringValue("o2"));

var context = new TemplateContext(options);
context.SetValue("o2", "new o2");
context.SetValue("o3", "o3");

Assert.Equal("o1", context.GetValue("o1").ToStringValue());
Assert.Equal("new o2", context.GetValue("o2").ToStringValue());
Assert.Equal("o3", context.GetValue("o3").ToStringValue());
}
}
}
3 changes: 2 additions & 1 deletion Fluid/TemplateContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;

namespace Fluid
{
Expand Down Expand Up @@ -33,7 +34,7 @@ public TemplateContext(TemplateOptions options)
{
Options = options;

LocalScope = new Scope();
LocalScope = new Scope(options.Scope);

LocalScope.SetValue("empty", NilValue.Empty);
LocalScope.SetValue("blank", StringValue.Empty);
Expand Down
5 changes: 5 additions & 0 deletions Fluid/TemplateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public class TemplateOptions
/// </summary>
public FilterCollection Filters { get; } = new FilterCollection();

/// <summary>
/// Gets a scope that is available in all the templates.
/// </summary>
public Scope Scope { get; } = new Scope();

/// <summary>
/// Gets the list of value converters.
/// </summary>
Expand Down

0 comments on commit e1555e3

Please sign in to comment.