Boost logo

Boost :

From: Rainer Deyke (rdeyke_at_[hidden])
Date: 2023-02-07 16:49:03


On 07.02.23 14:29, Rainer Deyke via Boost wrote:
> Reservation 1: The plural problem
> Reservation 2: The trailing comma problem

So after writing that long wall of text, I came up with a solution that
works in plain Mustache. I simply need to pass all my json through an
annotation function. This function modifies the incoming json as follows:
   - Any integer is replaced by a json object like this:
     {
       "value": value,
       "is_one": value == 1,
       "is_two": value == 2,
       "is_zero": value == 0,
       // more annotations as needed
     }
   - Any array is replaced with an array of json objects as follows:
     [
       {
         "value": org_array[0],
         "index": 0,
         "is_first": true,
         "is_last": false
       },
       {
         "value": org_array[1],
         "index": 1,
         "is_first": false,
         "is_last": false
       },
       // ...
       {
         "value": org_array[size - 1],
         "index": size - 1,
         "is_first": false,
         "is_last": true
       }
     ]

This allows the following mustache templates:

   "Found {{#num_files}}{{value}}
{{#is_one}}file{{/is_one}}{{^is_one}}files{{/is_one}}{{/num_files}} and
{{#num_dirs}}
{{#is_one}}directory{{/is_one}}{{^is_one}}directories{{/is_one}}{{/num_dirs}}."

   "Found these files:{{#files}}{{value}}{{^is_last}},
{{/is_last}}{{/files}}."

I wouldn't call this solution nice - there's a significant performance
cost involved - but I can live with it. I actually like this solution
better than any hack I could come up with using the lambda extension,
since it correctly works in nested contexts.

I no longer consider the lack of lambda extension support a valid reason
for not accepting this library into Boost.

-- 
Rainer Deyke (rainerd_at_[hidden])

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk