Skip to content

Commit

Permalink
List TemplateContext value names (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros authored Oct 15, 2021
1 parent ed65c47 commit e5c559d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Fluid.Tests/TemplateContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ public void SegmentAccessorCacheShouldVaryByType()
Assert.Equal("model1", template.Render(new TemplateContext(model1, options)));
}

[Fact]
public void CaptureShouldUpdateContext()
{
_parser.TryParse("{% capture greetings %}Hello {{text1}}{%endcapture%}", out var template, out var error);

var context = new TemplateContext();
context.SetValue("text1", "World");

template.Render(context);

Assert.Equal("Hello World", context.GetValue("greetings").ToStringValue());
Assert.Contains("greetings", context.ValueNames);
}

private class TestClass
{
public string Name { get; set; }
Expand Down
15 changes: 15 additions & 0 deletions Fluid/TemplateContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,26 @@ public void ReleaseScope()
}
}

/// <summary>
/// Gets the names of the values.
/// </summary>
public IEnumerable<string> ValueNames => LocalScope.Properties;

/// <summary>
/// Gets a value from the context.
/// </summary>
/// <param name="name">The name of the value.</param>
public FluidValue GetValue(string name)
{
return LocalScope.GetValue(name);
}

/// <summary>
/// Sets a value on the context.
/// </summary>
/// <param name="name">The name of the value.</param>
/// <param name="value">Teh value to set.</param>
/// <returns></returns>
public TemplateContext SetValue(string name, FluidValue value)
{
LocalScope.SetValue(name, value);
Expand Down

1 comment on commit e5c559d

@Soar360
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ValueNames property does not include parents scope.
Is this right ?

Please sign in to comment.