Skip to content
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

Allow for key references #298

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

indexzero
Copy link

We've been using this at Nodejitsu, and thought it might be useful mustache.js itself. Lets consider that you have a template the requires dynamic keynames like like:

foo.{{ refs.0 }}.foo: {{ foo.{{ refs.0 }}.foo }}
foo.{{ refs.1 }}: {{ foo.{{ refs.1 }} }}
{{ refs.2 }}: {{ {{ refs.2 }} }}

With a view:

  {
    "foo": {
      "first": { "foo": "bar" }
      "second": "second-value"
    },
    "third": "third-value"
    "refs": [
      "first",
      "second",
      "third"
    ]
  }

This would render:

  foo.first.foo: bar
  foo.second: second-value
  third: third-value

Thoughts? If you're interested in this I'll work up some tests.

@BigBlueHat
Copy link
Contributor

@janl remember discussing this "back in the day." :) I'd love to see this get merged--or something like it.

The syntax is a bit unwieldy and hard to read, but the objective (and the need for it) is spot on! Jan and I had discussed a syntax like {{@}} in blocks (this was prior to the dot pragma and the cascading scope being "stock" mustache, iirc).

Maybe a revised variation of the example above:

foo.{{ @.0 }}.foo: {{ foo.{{ @.0 }}.foo }}
foo.{{ @.1 }}: {{ foo.{{ @.1 }} }}
{{ @.2 }}: {{ {{ @.2 }} }}

I'd love the addition of something like this but for object key names as well:

{{#foo.@}}
- {{@.@}}
{{/foo.@}}

Result would be:

- first
- second

Syntax is humorous if nothing else. :)

Regardless of any of my blather, I'd love to see this idea reconsidered. Thanks for posting it (with code!) @indexzero

@melkior
Copy link

melkior commented Jan 6, 2014

Hi

This issue is closest to my problem. I need to traverse object the same way sections work. Why not allowing object traversal in similar manner as sections? For example:

{"obj": {"quux": "first", "foo": "second", "bar": "third"}}

{{@obj}} Object traversal code {{/obj}}

Now it would be really helpful that people could access keys and values. Maybe something like:

{{@obj}}
{{.}}: {{#.}} {{.}} {{/.}}
{{/obj}}

Would generate:

quux: first
foo: second
bar: third

This is kind in a line with mustache and syntax is better (?). Everything else should be made in javascript anyway.
What do you think?

Thanks

@MartinMuzatko
Copy link

I want this to happen <3

@caruccio
Copy link

caruccio commented Mar 5, 2014

I need this so bad!

@adapterik
Copy link

I implemented this in another c-template clone, in just this manner -- if a dotted elemented is referencing an array it will be used as an array index. I haven't actually had to use it often, but when needed it is indispensible. I have sometimes thought that using bracket syntax like refs[0] might be easier to read and understand, but the . was easier to implement and type.

@melkior I agree this can be very helpful. I implemented this using the * operator, because it reminded me that I was doing general purpose (and only) iteration. It operates for arrays, objects, and strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants