4 ms·
It is easy to get confused whether a brace is part of the json or part of the mustache/handlebars/etc. { "hello": {"{{place}}":{{{#items}}"{{thing}}":{{n
by badtuple 7y ago
It is easy to get confused whether a brace is part of the json or part of the mustache/handlebars/etc.
{
"hello": {"{{place}}":{{{#items}}"{{thing}}":{{n}},{{/items}}}},
"urls": [{{#items}}"{{url}}",{{/items}}]
}
It's unambigious and can be reasoned through, but it's certainly hard to tell what structure the template is building at a glance. This is not a complex or contrived example.
Syntax highlighting, linters, proper indentation, etc. all help...but when talking about non-programmers who aren't used to this it's an incredible hurdle. People are also lazy and error prone, so you can't rely on indentation to make things clear, especially since it's not always clear how you should indent nesting of the template vs nesting of the json datastructure you're trying to build.
Most modern templating languages have a config option for changing the delimeter. Setting it to something like (( or << might help differentiate things a bit, but then you're throwing away easy use of examples, documentation, stack overflow questions, and so much more that already exists for that language. That documentation and copy-pastable community help is not something to throw away lightly if you're designing a tool for this audience.
It's also worth pointing out that the above template generates subtly incorrect json with trailing commas. In pure mustache it's not possible to get rid of that trailing comma. You have to either change your data (add a "last":true key to the last element of any arrays), or have some kind of post-processing step.
All of the existing solutions are workable, they just aren't ideal.
- UncleEntity 7y agoI hacked together a asdl parser in boost::spirit a while back that used mustache to generate the C++ AST (which was then used to generate its own AST to make sure it all worked) and can attest the curly braces in the templates were a righteous PITA to get right. If I had to do it all over again I'd probably use the config option and change them to something like '(.', '.)' that doesn't naturally occur in the output language.