How to put {{#each}}...{{/each}} inside {{#ifCond}}...{{/ifCond}}?
See original GitHub issueI 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:
- Created 3 years ago
- Comments:9 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Bingo! That was it. Thank you so much!
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.