question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IndexOutOfRange Exceptions while passing partials parameters inside #each

See original GitHub issue

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.DictionaryIterator3.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.<Compile>b__0(TextWriter writer, Object context, Object data) at HandlebarsDotNet.HandlebarsEnvironment.<>c__DisplayClass16_0.<Compile>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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mattbruccommented, Feb 23, 2021

Ah, thank you very much, using Handlebars.Net.Extension.NewtonsoftJson solved it for me!

1reaction
mattbruccommented, Feb 18, 2021

Hey, thanks for the response @zjklee. I did reproduce on 2.0.4 as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - What is an IndexOutOfRangeException ...
This exception means that you're trying to access a collection item by index, using an invalid index. An index is invalid when it's...
Read more >
IndexOutOfRangeException Class (System)
The exception that is thrown when an attempt is made to access an element of an array or collection with an index that...
Read more >
IndexOutOfRangeException in C#
It occurs when an invalid index is used to access a member of a collection. The following example throws the IndexOutOfRange exception: Example:....
Read more >
C# static code analysis: General exceptions should never ...
Throwing such general exceptions as Exception , SystemException , ApplicationException , IndexOutOfRangeException , NullReferenceException ...
Read more >
How to handle an exception using try-catch
Learn how to handle an exception using a try-catch block. See a code example and view additional available resources.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found