Crow
Crow copied to clipboard
Mustache: Evaluate sections with context value '0' as false.
Under ActionType::OpenBlock:
case json::type::Number: if(ctx.num.ui == 0) current = action.pos; else stack.push_back(&ctx); break;
Could you please elaborate on why this addition is needed? Thank you
Sorry new to this if this isn't a neat format. in C++ 0 is always false
Sections render blocks of text one or more times, depending on the value of the key in the current context.
A section begins with a pound and ends with a slash. That is, {{#person}} begins a "person" section while {{/person}} ends it.
The behavior of the section is determined by the value of the key. False Values or Empty Lists
If the person key exists and has a value of false or an empty list, the HTML between the pound and slash will not be displayed.
Template:
Shown. {{#person}} Never shown! {{/person}}
Data:
{ "person": false }
Output:
Shown.
The specification seems to mention the term "false value", which in C++'s case would include 0. Thank you for reporting this issue, we'll get it fixed ASAP. Alternatively, you could open a PR and we'll merge it (which would be faster).
Thank you, I appreciate all the work you've done with this project :)
Thanks for the kind words!
One thing to mention regarding your solution, it assumes the number type is unsigned, which would be incorrect if the type was instead signed or floating point. Crow doesn't cast the number value to unsigned when getting num.ui. if the number type was not the type requested, 0 (or 0.0) would be returned.
Checks for the different types need to be added, possibly using a switch statement for ctx.num_type
You are right:
case json::type::Number:
switch (ctx.nt)
{
case json::num_type::Signed_integer:
if(ctx.num.si == 0)
current = action.pos;
else
stack.push_back(&ctx);
break;
case json::num_type::Unsigned_integer:
if(ctx.num.ui == 0)
current = action.pos;
else
stack.push_back(&ctx);
break;
case json::num_type::Floating_point:
if(ctx.num.d == 0.0)
current = action.pos;
else
stack.push_back(&ctx);
break;
default: //num_type == Null
current = action.pos;
break;
}
sorry i gotta figure out how this formatting works