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

accessing.stuff.with.dots #6

Closed
ncr opened this issue Oct 7, 2009 · 25 comments
Closed

accessing.stuff.with.dots #6

ncr opened this issue Oct 7, 2009 · 25 comments
Labels

Comments

@ncr
Copy link

ncr commented Oct 7, 2009

It would be cool to be able to do {{user.name}} (dot notation access) so it would return user[:name] where user is a method in view returning a Hash. It should also accept statements with more dots like: {{user.address.city}}.

@defunkt
Copy link
Member

defunkt commented Oct 9, 2009

I'm not sure about this yet - I really like how simple the template system is right now and don't personally have a problem just defining methods for more complicated objects. That way if the object's structure changes it can be caught by your unit tests and only needs to ever be changed in your Ruby code.

@ncr
Copy link
Author

ncr commented Oct 11, 2009

This testing argument caught me too - adding dot syntax would make those views more prone to bugs.

@defunkt
Copy link
Member

defunkt commented Oct 11, 2009

Great - closing this until it warrants further discussion in the future.

@jeremychone
Copy link

I am missing something. How do you get back up in a view in the template. Let's say, you need to render user.addressbook.contacts, and after this, you would like to display the user.name. I would you do that?

@defunkt
Copy link
Member

defunkt commented Aug 23, 2010

{{name}} will go up the stack

@jeremychone
Copy link

ok, I am not sure I understand how the system work then. Mustache is definitely cool, but not having the "dot" notation is kind of a blocker for us.

@jeremychone
Copy link

BTW, do not get me wrong, I think mustache.js is very cool, but in our case it pushes the "logicless" a little bit too far. We are settling for jquery-tmpl for now, but we will definitely keep an eye on mustache.js

@al3xandru
Copy link

There do seem to be a scenario which I cannot figure out how to solve:
parent: {
id: 'parent_id',
child: {
id: 'child_id',
val: 'child_value'
}
}

{{#child}}
/// how can I refer to the parent id?
{{/child}}

@jeremychone
Copy link

+1, this is exactly what I am confused about. Would love to see the answer.

@lukemorton
Copy link

You have an object like this:
parent: {
id: 'parent_id',
child: {
id: 'child_id',
parent_id: 'parent_id',
val: 'child_value'
}
}

@jeremychone
Copy link

So, just that I understand. The solution to access parent values from a {{#child}} element is to duplicate the parent values in the child element. Correct?

@lukemorton
Copy link

That's the way I would solve you're problem. I'm not saying it's the most DRY use of data.

@mhevery
Copy link

mhevery commented Oct 30, 2010

For those who can't live without the dot notation (such as myself) here is a branch: http://github.com/mhevery/mustache.js/tree/dot-notation

For what it is worth, the dot notation is a must since your data often comes from objects which are structured, and which you don't controll. Flattening them would mean that I would have to write marshaling code, which would mean more code to write and more code to test. Hence, I don't buy the testability argument.

@audionerd
Copy link

Here's an example of parent data accessed within a child without dot notation:

https://gist.github.com/3c91441597ac650146b9

via: http://librelist.com/browser//mustache/2010/10/16/nested-names/#7f58fcf41fa5a60259932d1efcfb6f60

@jeremychone
Copy link

@audionerd thanks for the example. Honestly, I moved to jquery-tmpl. The lack of dot.notation was to much work to work around. It added lot of complexity to rather a simple use case.

@OscarGodson
Copy link

I've moved to jQuery Tmpl also and I'm liking underscore.js also which supports the same syntax:
http://documentcloud.github.com/underscore/#template

@jeremyheiler
Copy link

What about having a dot notation similar to navigating a directory structure.

For example:

{
  "name" : "Jerry",
  "friends" : [
      { "name" : "Kramer" },
      { "name" : "Eliane" },
      { "name" : "George" }
  ]
}

{{#friends}}
  {{.name}} is friends with {{name}}
{{/friends}}

Would result in:

Jerry is friends with Kramer
Jerry is friends with Eliane
Jerry is friends with George

Essentially you would provide a dot for each parent conext you want to look at. So, {{..foo}} would look at the context two levels deep in the stack. I'm not sure if this would conflict with {{.}}. Maybe on a conceptual level it would.

@defunkt
Copy link
Member

defunkt commented Mar 22, 2011

Dot notation has been added to Mustache.

@antz29
Copy link

antz29 commented Jul 7, 2011

@defunkt where is that? I can see it anywhere. :S

@defunkt
Copy link
Member

defunkt commented Jul 7, 2011

c183699

@ghost
Copy link

ghost commented Aug 23, 2011

Seems like the FAQ answer to:
"Why doesn’t Mustache allow dot notation like {{variable.member}}?"
can be updated.

@turadg
Copy link

turadg commented Oct 11, 2011

@defunkt, @t-nunes is right. README is confusing currently.

handlebars.js README should be updated too to stop claiming dot-notation is a difference.

@snimavat
Copy link

What is the syntax for dot notation?

@daonb
Copy link

daonb commented Mar 19, 2012

I'm just getting into Mustache and while the missing dots confuse me, I'd rather do without them. I think it force knowledge out of the template and into the view and I like it. It's confusing because t's not what I'm used to with Django, but that's why our project is migrating to mustache - logicless templates that will keep my templates clean and make it possible to get web designers to contribute template code.
IMO, the samples should stay the same - without dot notation and the FAQ should have two answers: @audionerd gist and the fact that it works but it's not the mustache way

@jeremychone
Copy link

@daonb I think they have added dot support now. However, I am not sure that dot-notation makes the template not as clean. In fact, I am still not convince that the lack of if/else is good either. There is a fine line that you want to draw between what the template needs to do and what need to do before hand (before processing the template). Having to do too much "pre-hashing" before template rendering can make the code more complex that it has to be.

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

No branches or pull requests