Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Mustache API call to extract key names? #134

Open
bluemont opened this Issue Jun 6, 2012 · 7 comments

Comments

Projects
None yet
5 participants
@ghost

ghost commented Jun 6, 2012

I asked this question on Stack Overflow first, but now I think I should ask it over here:

m = Mustache.new
m.template = "Hello {{first_name}} {{last_name}}"

I want to make an API call -- but I don't know what it is -- that returns the key names:

[:first_name, :last_name]

From what I can tell, this is not supported.

Contributor

pvande commented Jun 7, 2012

There isn't a way specifically to do that, but as a start you might consider the following:

>> pp Mustache::Template.new('Hello {{first_name}} {{person.last_name}}').tokens
[:multi,
 [:static, "Hello "],
 [:mustache, :etag, [:mustache, :fetch, ["first_name"]]],
 [:static, " "],
 [:mustache, :etag, [:mustache, :fetch, ["person", "last_name"]]]]
@ghost

ghost commented Jun 7, 2012

Thanks. I'd appreciate suggestions like the one above over on the SO question so I can give credit and raise awareness.

I went with a bit simpler and more naive approach

template = "{{result1}} and {{result2}} was twice the size of {{result3}}"
template.scan(/{{(\S+)}}/).flatten #=> ["result1", "result2", "result3"]

Granted, that will miss triple mustaches, but c'est la vie.

I've just went ahead and implemented this in my fork bf23c26 in branch feature__list_tags.

Three new methods of Template now available there are: tags, sections and partials. All of them return a flat array of strings.

Tags within sections are prefixed using section.tag notation.

If you want to do some other crazy traversal of the token tree, there's an class method of Template named recursor too.

Owner

locks commented Apr 17, 2014

@steakknife did you send a PR back to this fork?

@locks locks closed this Apr 17, 2014

@locks locks reopened this Apr 17, 2014

@locks locks added the feature label Apr 17, 2014

Owner

locks commented Oct 22, 2014

@locks Unfortunately, I don't have the time right now.  If you or someone else to were to "steal" or rewrite the code into a PR, please be my guest.


Sent from Mailbox

On Tue, Oct 21, 2014 at 6:17 PM, Ricardo Mendes notifications@github.com
wrote:

@steakknife poke

Reply to this email directly or view it on GitHub:
#134 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment