groue / GRMustache.swift

Flexible Mustache templates for Swift
http://mustache.github.com/
MIT License
597 stars 155 forks source link

Indexed arrays don't appear to work #2

Closed edwardaux closed 9 years ago

edwardaux commented 9 years ago

Hi... I'm using the Swift1.2 branch, and am having trouble referencing array values via a direct index. eg. {{list.0}}. The following just outputs an empty string:

let input = [
    "list" : [ "aaa", "bbb" ]
]
let template = Template(string: "{{list.0}}")!
println(template.render(Box(input))!)

Interestingly, however, if I use {{list}}, then it outputs aaabbb. Just curious if fetching data from arrays using an index is supported?

Of course, it could also be that (as a Mustache newbie) I am using the wrong notation.

Thanks.

edwardaux commented 9 years ago

Just doing a bit more fiddling, and it seems that referencing a dictionary by index seems to work. This means I can sort of support what I want by mapping my array input into a dictionary input:

let input = [
    "list" : [ "0":"aaa", "1":"bbb" ]
]
let template = Template(string: "{{list.0}}")!
println(template.render(Box(input))!)

Not quite as pretty, but manageable I guess.

groue commented 9 years ago

Hi @edwardaux.

list.0 is not standard Mustache, and GRMustache.swift does not support that feature.

You can use first and last if you want a quick access the first and last item of an array. Relevant documentation

groue commented 9 years ago

I dislike a lot the non-standard syntax list.0, because even if it comes from the famous mustache.js, at the same time it jeopardizes any sane effort to introduce much needed features such as support for literals (context).

edwardaux commented 9 years ago

Thanks for the quick reply @groue

Now that you mention it, my googling to find the list.0 notation was indeed from the mustache.js documentation.

While first and last are probably useful in the general case, my personal requirements need to be able to fetch the n'th element. No matter... as I said I can just switch to using a dictionary instead of an array.

Thanks again

groue commented 9 years ago

I'm happy you could find a workaround. Happy Mustache!

groue commented 9 years ago

@edwardaux FYI there is no Swift1.2 branch any longer: the master branch itself has switched to Swift1.2.

edwardaux commented 9 years ago

Thanks for the pointer. Updating now.

groue commented 9 years ago

Cool. Were you able to have Carthage working? I've tried and failed so far.

edwardaux commented 9 years ago

Yep, working for me - note, though, I was building a Mac OS X app (not iOS) using Xcode 6.3beta4 pointing to Swift1.2 branch.

I have downloaded Xcode 6.3 GA, but haven't had a chance to repoint to master branch and retest. I expect it should work OK though.

What problems are you having?

groue commented 9 years ago

I don't exactly remember now - it was a few weeks ago. It would get in a complete muddle in the middle of the two targets, the one for iOS and the one for OSX. But if you could do it, I could do it as well. I'll just try again. Thanks for telling me you did it, that's a light of hope :-)