-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Displaying section if array exists, *without* iterating each element of array. #1
Comments
According to this page... ...Mustache.js was updated to allow {{a.0}}, while handlebars.js has been updated to allow {{a.[0]}}. From my perspective, I think this is a very basic omission from vanilla Mustache, because there are innumerable cases where you would need this (e.g. to render a preamble to a list conditional on the list existing, etc). The direct problem for me is that I'd really like to use Magnum to automate some build tasks, but its (entirely reasonable) lack of Lambdas means that I can only use vanilla Mustache. Hence I'd be happy to put some work in to fix this if you're OK with that? From looking at the code, it looks as though the least intrusive change would be to h(ij)ack the find() function in src/magnum.c so that if the last dot-delimited section of the name is numeric (i.e. index) and the preceding part of the name refers to an array, it instead returns the object array[index]. Does that make sense to you? |
As an aside, the Parson author already considered adding code to handle more general array[n] cases, but decided not to do this (because of a memory leak?): |
@nickpelling -- thanks for the comments. And forgive the length of this -- it's partially me just thinking out loud, and partially recording my thoughts for future consideration when I decide I was wrong. ;) In the almost 2 years since I started magnum, I have been using it quite a bit as is (though have not needed to push many updates, there are one or two edge case bugs I need to track down related to comments, and I am currently working on creating fuzzers to stress test most of my software projects to identify more bugs). In that time, I have come to appreciate the "stated design philosophy as I understand it" of Mustache (the syntax). IF you consider Mustache as a tool to process a JSON object as it is given to you, then there are some clear limitations of the syntax that create troublesome situations (this being one of them.) Alternatively, IF you consider the workflow to be:
Then some of their design decisions make more sense. My most common use case over the last 2 years has been "templated programming" -- my unofficial term for trying to pull out repetitive parts of programming into templates, that are then used to build the desired source code via Magnum/JSON/template arrangements. For example, I created templates that:
In doing this, I would run up against limits of Mustache only to realize that if I redesigned my JSON, there was no problem. For example, I started out my database idea by using a Similarly, a common situation in my uses is that I need to do something different on the last element of an array as compared to all the other elements (usually whether or not to append a comma). To solve this, I have to remember to include a In my mind, the ultimate question becomes, "How complicated do you make the Mustache syntax in order to accept generic JSON input, or how much care do you require users to take when designing their JSON so that it works properly with Mustache?" Since I am not the maintainer of Mustache, I obviously don't have the answer. My opinion is that there is benefit to a bare bones, but consistent, feature set for the Mustache syntax across all implementations. Individual implementations will, of course, have some custom features as needed for their particular use cases, but hopefully the core syntax will work across them all. For magnum, this means:
So far, the custom features that have come up for discussion in magnum are:
As above, I am still considering these, and do not have a final decision. I also have a lot pressing tasks in my personal, professional, and software lives at the moment, so don't expect any changes soon. ASIDE: At some point, tools like jq can process JSON that you have less control over to add the necessary flags to be compatible with Mustache. So there are solutions to just about any problem one encounters. |
For me, JSON is a great way of decoupling data from code, and Mustache is a great way of generating all manner of useful stuff from JSON. But the minute you start having to add isFirst and/or isLast flags into the data, the tail is wagging the dog. If you're having to make implicit stuff explicit to get by, something has gone badly wrong. Personally, I think that Mustache was right to aim to stay minimal: but even so, there is a very strong case for #IsFirst and #IsLast built-in wrappers. These aren't "logic", they are template sequencing helpers that you can use to do preambles and remove trailing commas {{^IfLast}},{{/IfLast}} etc. |
Given JSON:
and template:
Some Mustache implementations (including the Demo here return:
The
{{#images.length}}
tag does not appear to be part of the Mustache spec. For that matter, neither does{{#images.0}}
which could be used to refer to the first element of the array.It seems that we are in another "Markdown" scenario where the initial parent project is at least partially dead, whereas one or more child projects are still alive (including this one.) Which is unfortunate.
I will probably end up implementing these features, as they seem useful to me, and do not seem to violate the logic-less philosophy as demonstrated by other core features. But I don't have time at the moment, so this is a reminder to myself.
The text was updated successfully, but these errors were encountered: