IndexOutOfRange Exceptions while passing partials parameters inside #each
Closed this issue · 5 comments
Describe the bug
I have found I am unable to pass partials parameters while inside #each loops. I have distilled this down to a reproducible test.
ndlebarsDotNet.ObjectDescriptors.ObjectDescriptorProvider.<>c.<.cctor>b__7_0(ObjectDescriptor descriptor, Object o)
at HandlebarsDotNet.BindingContext.PopulateHash(HashParameterDictionary hash, Object from)
at HandlebarsDotNet.BindingContext.Initialize()
at HandlebarsDotNet.BindingContext.BindingContextPool.CreateContext(ICompiledHandlebarsConfiguration configuration, Object value, BindingContext parent, TemplateDelegate partialBlockTemplate)
at HandlebarsDotNet.Iterators.DictionaryIterator3.Iterate(EncodedTextWriter& writer, BindingContext context, ChainSegment[] blockParamsVariables, Object input, TemplateDelegate template, TemplateDelegate ifEmpty) at HandlebarsDotNet.Iterators.DictionaryIterator
3.HandlebarsDotNet.Iterators.IIterator.Iterate(EncodedTextWriter& writer, BindingContext context, ChainSegment[] blockParamsVariables, Object input, TemplateDelegate template, TemplateDelegate ifEmpty)
at HandlebarsDotNet.Compiler.Iterator.Iterate(BindingContext context, EncodedTextWriter writer, ChainSegment[] blockParamsVariables, Object target, TemplateDelegate template, TemplateDelegate ifEmpty)
at HandlebarsDotNet.HandlebarsEnvironment.<>c__DisplayClass15_0.b__0(TextWriter writer, Object context, Object data)
at HandlebarsDotNet.HandlebarsEnvironment.<>c__DisplayClass16_0.b__0(Object context, Object data)
at csi.HTMLRender.Tests.HandlebarsTests.HandlebarsHelperTests.EachInsideIfConditionTest() in C:\repos\csi.HTMLRender\csi.HTMLRender.Tests\HandlebarsTests\HandlebarsHelperTests.cs:line 382
Expected behavior:
Exception shouldnt be thrown, and should be able to pass parameters to partials while inside an each loop
Test to reproduce
[Fact]
public void PartialParametersInsideEachTest()
{
string source = @"
{{#each itemType}}
{{>partial item=this}}
{{/each}}
";
string partialContent = @"{{#if First}}printValue{{/if}}";
Handlebars.RegisterTemplate("partial", partialContent);
var template = Handlebars.Compile(source);
var data = new
{
itemType = new JObject()
{
["1"] = "1",
["3"] = "3",
["5"] = "5",
["7"] = "7"
}
};
var result = string.Empty;
try
{
result = template(data);
}
catch (System.IndexOutOfRangeException ex)
{
}
Assert.IsTrue(result.Contains("printValue"), "Result does not contain 'printValue'");
}
Other related info
handlebarsdotnet v 2.02,
Tested using .net core 3.1
After submitting the issue
Please consider contributing to the project by submitting a PR with a fix to the issue.
This would help to solve your problem in a shorter time as well as help other users of the project.
In case you do not know where to start - feel free to ask for help in the issue thread.
Building an active community is essential for any project survival as time of maintainers is limited.
Hello @mattbruc
Have you tried to reproduce the problem on the latest version (2.0.4)?
Hey, thanks for the response @zjklee. I did reproduce on 2.0.4 as well.
Looks similar to #422
I'd try to have a look once I have time.
In any case PR is welcome ;)
@mattbruc, after some digging it looks like the problem is related to the fact that you're using JObject
. In case this is intended use I'd suggest you use Handlebars.Net.Extension.NewtonsoftJson
Ah, thank you very much, using Handlebars.Net.Extension.NewtonsoftJson solved it for me!