Crow icon indicating copy to clipboard operation
Crow copied to clipboard

Mustache: Evaluate sections with context value '0' as false.

Open IllicitFrog opened this issue 3 years ago • 6 comments

Under ActionType::OpenBlock:

case json::type::Number: if(ctx.num.ui == 0) current = action.pos; else stack.push_back(&ctx); break;

IllicitFrog avatar Jul 04 '22 18:07 IllicitFrog

Could you please elaborate on why this addition is needed? Thank you

The-EDev avatar Jul 06 '22 00:07 The-EDev

Sorry new to this if this isn't a neat format. in C++ 0 is always false

Sections

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.

IllicitFrog avatar Jul 06 '22 02:07 IllicitFrog

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).

The-EDev avatar Jul 06 '22 03:07 The-EDev

Thank you, I appreciate all the work you've done with this project :)

IllicitFrog avatar Jul 06 '22 03:07 IllicitFrog

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

The-EDev avatar Jul 06 '22 03:07 The-EDev

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

IllicitFrog avatar Jul 06 '22 04:07 IllicitFrog