-
Notifications
You must be signed in to change notification settings - Fork 59
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
Support {{#.}} and Iterable values #2
Comments
Good idea regarding support of Iterables. Could you give me an example of the {{#.}} feature. I haven't seen that feature before. Is it related to implicit iterators? For example:
|
I'm not entirely sure if it's a "standard" Mustache feature. It is in this Java port though: https://github.com/spullara/mustache.java (I could understand if you don't want to add something that isn't standard Mustache.) It's for when the thing you're passing to template.renderString is itself a List (or Iterable). Like this: data: [ 'a', 'b', 'c', 'd', 'e' ] |
Ok - I see. That saves allocating a map in your use case. I'm not opposed to adding it, especially if it's already supported in other mustache libraries. I suppose this should work as well then? data: {'list': [ 'a', 'b', 'c', 'd', 'e' ]} And perhaps even this? (but that makes my head hurt) data: {'list': [ 'a', 'b', 'c', 'd', 'e' ]} |
Hmm... I think the second one is wrong, and the first one is right, but not for the reason you think. The second one should be:
The dot is like a 'this' keyword. In that example, it appears inside "{{#list}}", so the dot refers to the current entry in the list. Then, since the current entry is a string, "{{#.}}" is basically an if statement testing that the string is truthy (a.k.a. non-empty). If it is truthy (which they all are), then it prints 'this', which is still the string. Another example:
I didn't actually test that with the Java Mustache library but I'm pretty sure that's how it works. |
Oops. My mistake. I wanted to make sure the feature nests correctly. Thanks On Wed, Jun 12, 2013 at 3:45 PM, michaelhixson [email protected]:
|
Heh, well I'm talking about what it should do. My code in the OP might be doing something else. I've been a bad/lazy contributor by not adding unit tests. |
I confirmed that mustache.js has the expected behavior for the 2 examples I gave. https://github.com/janl/mustache.js This library (with my changes) gets the second example right, but it fails on the first example.
I had to add if statements to _renderSection and _renderInvSection for dealing with the "(value is String)" case. _renderSection:
_renderInvSection:
That made it produce the expected output. Not sure if truthy strings are actually part of the Mustache spec or if I just made that up. If I just made it up, then I guess it's supposed to throw an error, and this change isn't necessary? |
Is this relevant?
|
Ah. So, truthy/falsey is left to the language. And Dart does not do coercion of non-booleans to booleans, right? In that case, the code from my last comment should not be added, and the error is correct. |
I think matching ruby's mustache library is the best bet. Taken from the bug linked above:
|
Yikes - but don't want to match this behaviour. Weird!
|
I think it would be best to follow the advice of @pvande.
That is, do not add any notion of string truthiness. Dart has no such thing and neither does (generic) Mustache. A section that resolves to a string value should throw an error, as it already does. That's just my... vote. (Is this library a democracy? :) |
Yup - sounds good to me. Btw - if you want to add yourself to the authors list in the pubspec.yaml file - go for it. |
See the following commit for a small change that adds support for:
https://github.com/michaelhixson/mustache/commit/ce331cc921da62d6aadc3569a890120be682e580
Why? It removes the need for some extra map/list creation in code that uses this library. For instance, in this code, I want to pass a list of simple model objects to my template:
https://github.com/TechEmpower/FrameworkBenchmarks/blob/master/dart/server.dart#L195-L198
Currently I have to insert that as the value in a single-entry map, so that it has a name the template understands (but I'd rather avoid that and use "{{#.}}"). Also, I have to add a "toList()" to turn my Iterable values into a List, or else the template doesn't know how to render it (but I'd rather just avoid doing that -- the template doesn't really need it to be a List).
The text was updated successfully, but these errors were encountered: