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.

How to put {{#each}}...{{/each}} inside {{#ifCond}}...{{/ifCond}}?

See original GitHub issue

I created an “ifCond” helper like in the tests, and would like to put an {{#each}} iterator inside one of the branches and am having trouble. Not sure if the “ifCond” helper is wrong or what. nesting {{#each}} inside a {{#if}} works fine, but I need more control over what constitutes a true in the {{#if}}

here is my “ifCond” helper:

            compiler.RegisterHelper("ifCond", (writer, options, context, args) => {
                if (args.Length != 3)
                {
                    writer.Write("ifCond:Wrong number of arguments");
                    return;
                }
                if (args[0] == null || args[0].GetType().Name == "UndefinedBindingResult")
                {
                    writer.Write("ifCond:args[0] undefined");
                    return;
                }
                if (args[1] == null || args[1].GetType().Name == "UndefinedBindingResult")
                {
                    writer.Write("ifCond:args[1] undefined");
                    return;
                }
                if (args[2] == null || args[2].GetType().Name == "UndefinedBindingResult")
                {
                    writer.Write("ifCond:args[2] undefined");
                    return;
                }
                if (args[0].GetType().Name == "String" || args[0].GetType().Name == "JValue")
                {
                    var val1 = args[0].ToString();
                    var val2 = args[2].ToString();

                    switch (args[1].ToString())
                    {
                        case ">":
                            if (val1.Length > val2.Length)
                            {
                                options.Template(writer, (object)context);
                            }
                            else
                            {
                                options.Inverse(writer, (object)context);
                            }
                            break;
                        case "=":
                        case "==":
                            if (val1 == val2)
                            {
                                options.Template(writer, (object)context);
                            }
                            else
                            {
                                options.Inverse(writer, (object)context);
                            }
                            break;
                        case "%=": // case-insensitive comparison
                            if (val1.Equals(val2, StringComparison.OrdinalIgnoreCase))
                            {
                                options.Template(writer, (object)context);
                            }
                            else
                            {
                                options.Inverse(writer, (object)context);
                            }
                            break;
                        case "<":
                            if (val1.Length < val2.Length)
                            {
                                options.Template(writer, (object)context);
                            }
                            else
                            {
                                options.Inverse(writer, (object)context);
                            }
                            break;
                        case "!=":
                        case "<>":
                            if (val1 != val2)
                            {
                                options.Template(writer, (object)context);
                            }
                            else
                            {
                                options.Inverse(writer, (object)context);
                            }
                            break;
                        case "%!=":
                        case "%<>":
                            if (!val1.Equals(val2, StringComparison.OrdinalIgnoreCase))
                            {
                                options.Template(writer, (object)context);
                            }
                            else
                            {
                                options.Inverse(writer, (object)context);
                            }
                            break;
                    }
                }
                else
                {
                    var val1 = float.Parse(args[0].ToString());
                    var val2 = float.Parse(args[2].ToString());

                    switch (args[1].ToString())
                    {
                        case ">":
                            if (val1 > val2)
                            {
                                options.Template(writer, (object)context);
                            }
                            else
                            {
                                options.Inverse(writer, (object)context);
                            }
                            break;
                        case "=":
                        case "==":
                            if (val1 == val2)
                            {
                                options.Template(writer, (object)context);
                            }
                            else
                            {
                                options.Inverse(writer, (object)context);
                            }
                            break;
                        case "<":
                            if (val1 < val2)
                            {
                                options.Template(writer, (object)context);
                            }
                            else
                            {
                                options.Inverse(writer, (object)context);
                            }
                            break;
                        case "!=":
                        case "<>":
                            if (val1 != val2)
                            {
                                options.Template(writer, (object)context);
                            }
                            else
                            {
                                options.Inverse(writer, (object)context);
                            }
                            break;
                    }
                }
            });

What am I doing wrong, or is this just not possible?

Thanks.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
mrbelkcommented, Mar 19, 2021

Bingo! That was it. Thank you so much!

1reaction
mrbelkcommented, Mar 19, 2021

I will do my best. I am under the gun a little atm with a deadline for a customer. I’ll try to whip something up today.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handlebar if condition in each loop - javascript
Am using handlebar js to make a selectbox. <select id="order_status_selection"> {{#each orders_status}} <option value="{{id}}" {{#ifCond id ...
Read more >
Built-in Helpers
You can iterate over a list using the built-in each helper. Inside the block, you can use this to reference the element being...
Read more >
Handlebars unable to pass array value by index to helpers
{{#ifCond this.3. '!=' 301}} will pass a number rather than a string.
Read more >
3.12 Conditionals: if, cond, and, and or
Evaluates test-expr. If it produces any value other than #f, then then-expr is evaluated, and its results are the result for the if...
Read more >
Data object with array of objects The code examples ...
Handlebars count each. The first argument to this helper is the array to be iterated, and the value being iterated is yielded as...
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