How to render a section once for a nonempty list? #157
Replies: 7 comments 29 replies
-
To continue discussion of originally #147
You don't have to apologize! I have wavered back and forth on not liking the virtual key approach for sometime from a purist and academic of which I am naturally and I do agree it is nasty and special case possibly on the verge of bloat. However this is less about syntactically/semantically expressibility and more about "standard builtins". If we don't provide a standard set of filters (or power lambdas) it is the exact same problem. And I largely disagree on the collision problem because inner tag name content was largely never specified you very well could have tag names (key) of Let us go back to the user who is not so comfortable writing lambdas or filters or is not even allowed to. Which filter do they use to mimic So I'm in agreement with @spullara in that it would be nice to have an optional/recommended enhancement to the spec. Even just called best practice. Not a requirement but a recommendation of these keys could be special. If you are a new implementer consider not allowing them for general use. After all you are saying the same thing with
Look I'm from Java camp. Java is late mover strategy to the max but at least Java has a ton of builtins (however it is massively still lacking the complaints are still high). When I said handlebars has precedence I actually mean almost every templating language in the Java ecosystem has For example:
(I can keep adding to the list later if it helps as there are more than the above). I just want to keep some perspective that Mustache is used by many people that are less technical and largely successful precisely because it does not have a lot of logic to teach. Also one of its greatest impacts of the logicless is that it is possible to make it much more secure in environment with non-platform developers. I'm not saying filters cannot be constrained but it will increase surface area. A binding is a lot simpler than a filter and I think virtual keys largely can be done today for most implementations without changing the tokenizer. To @spullara point while it is special case it is less "logic" than bindings which is ultimately at the spirit of Mustache. I'm very wary of things becoming like XSLT or Scheme. Pure but the damn batteries are missing. |
Beta Was this translation helpful? Give feedback.
-
I'm still of the opinion that the best option is for people using mustache to default to putting the logic in the backing code and just get used to that or use a wrapper around lists. Barring that, there is at least one easy option that is missing: {{^^list}} - {{^list}} already means "{{#list.isEmpty}}.." and if we generally think ^ means negation this could be a simple solution with unlikely overlap with current templates. |
Beta Was this translation helpful? Give feedback.
-
@jgonggrijp Thanks for bringing this up, I was about to open a new discussion/ticket today on this topic! My personal preference; 3, 8, 5. I'm using an implementation that has 3 - https://github.com/clach04/stache#existence-check-exists (implementation from https://github.com/hyperturtle/Stache) 5 has the potential to cause problems for some existing templates (I do not use variable markers with a question mark prefix BUT it is possible, albeit maybe not plausible 😉). Suggestions for updating the table for: 1 - A variant of this is described in https://stackoverflow.com/questions/11653764/mustache-how-to-detect-array-is-not-empty/70478815#70478815 - basically you have a single value hash/dictionary with a value of a list - which leads to nesting in the template (I've not tried this as I'm happy with 3 implementation I've been using the past few years). 2 - .length appears to be common in js implementations. Related discussions; janl/mustache.js#683, robertknight/qt-mustache#22, #138, mustache/mustache#118 |
Beta Was this translation helpful? Give feedback.
-
Posed to #171
Those numbers were not coincidences 😉, I was suggesting updating the table with those additional notes at those table positions. For example:
|
Beta Was this translation helpful? Give feedback.
-
Not life changing :-D
Will do. |
Beta Was this translation helpful? Give feedback.
-
I'd like to see 9 implemented, I like the idea and the syntax... but it's not a complete solution for me (more details). I can see myself using an I still need/want a Alternative idea which merges 3 and 9 into an {{?list}} No changes to existing implementations, so no surprises. You have to opt in to use the new syntax/feature. |
Beta Was this translation helpful? Give feedback.
-
the syntax of i see the further evolution of
here, the case of my preference rejects putting boolean values onto the stack
encourage the use of helpers, doing pre-analysis of the data, which will reduce the logic in templates and make them more readable/understandable/friendly. i see no cases where pre-processing is not available, even in the playground, data may be automatically analyzed and a hashmap with name postifixes (like |
Beta Was this translation helpful? Give feedback.
-
A regular section can render conditionally and will iterate over a list. Here lies a problem: what if you want to render the section exactly once if the list is nonempty, regardless of the length of the list? This would make it possible to render, for example, a
<ol>...</ol>
wrapper if a list is nonempty but not if the list is empty.This question has come up many times. Some of the previous discussions include #14, #109, #147 and #152. #153 and #155 are relevant as possible solutions.
In #147 (comment), I made the following table of possible ways to address the use case. I suggest centralizing further discussion here and will keep the table updated.
{{#listNonEmpty}}{{/listNonEmpty}}
){{#list.0}}{{/list.0}}
,{{^list.isEmpty}}{{/list.isEmpty}}
, ...){{?list}}{{/list}}
){{#nonEmpty.list}}{{/nonEmpty.list}}
or similar){{#?list}}{{/?list}}
){{# list | nonEmpty }}{{/ list | nonEmpty }}
){{#list}}{{#@first}}{{/@first}}{{/list}}
){{^^list}}{{/^list}}
){{^list}}{{|}}here{{/list}}
)Option 5 is what was proposed in #147. Option 9 was proposed in #171 (comment). At the time of writing, my personal ranking of preference would be 6>9>4>7>1>5>8>2>3.
Beta Was this translation helpful? Give feedback.
All reactions