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

Dynamic property access with mustache and stache #1157

Closed
zkat opened this issue Jul 8, 2014 · 3 comments
Closed

Dynamic property access with mustache and stache #1157

zkat opened this issue Jul 8, 2014 · 3 comments

Comments

@zkat
Copy link
Contributor

zkat commented Jul 8, 2014

VOTE HERE

I've been working on converting a lot of EJS templates to mustache recently, and have found one particular pattern that I think might be worth addressing with a built-in solution.

Right now, without writing your own helper, you can't access a data property in mustache based on another property in the scope. Something like <%= car.attr(part); %> in EJS has no direct equivalent in mustache. To pull off the same task, you would need to write a helper, but that is only of limited utility.

This, for example, would still be fairly hard to translate:

<% things.attr(key).each(function(keyedThing) { %>
...
<% } %>

My first instinct is to propose a syntax extension that follows JavaScript conventions for dynamic field access: obj[key], so you would be able to write the following to translate the above without losing the utility of #each:

{{#each things[key]}}
...
{{/each}}
@justinbmeyer justinbmeyer added this to the 2.2.0 milestone Jul 23, 2014
@justinbmeyer
Copy link
Contributor

That seems fine to me. @andykant ? I would check mustache / Handlebars for any existing conventions.

Added this to the 2.2 milestone.

@justinbmeyer justinbmeyer changed the title Dynamic property access with mustache Dynamic property access with mustache and stache Aug 25, 2014
@zkat
Copy link
Contributor Author

zkat commented Dec 9, 2014

http://handlebarsjs.com/builtin_helpers.html Mentions a helper called lookup. I think in order to really be able to use something like that, we'd need nested helper support. The following then would do the trick nicely while still making [] available for escapes (see #1344) and fitting in more with how handlebars does things. I don't know if there's an issue for that yet, and searching failed me, but see:

{{#each (lookup things key)}}
...
{{/each}}

Either way, probably good for us to include the lookup helper by default since it's default in handlebars.

@justinbmeyer
Copy link
Contributor

Closing for: canjs/can-stache#7

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

No branches or pull requests

3 participants