From 3ca6d1d43580a3fa98c314250ecacc602a8b263a Mon Sep 17 00:00:00 2001 From: Tim Harper Date: Wed, 9 Nov 2011 00:38:15 -0700 Subject: [PATCH 1/2] README: Clarify Deferencing Section docs It was confusing to present the concept of dereferencing a nested object and enumerating over a list in an same example intended to show dereferencing a nested object. I believe this example illustrates the concept more clearly. --- README.md | 57 ++++++++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 64096b5..7b134b2 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ enumeration section. If a section key returns a function, it will be called and passed both the unrendered block of text and a renderer convenience function. -Given this JS: +Given this object: "name": "Tater", "bolder": function() { @@ -115,39 +115,36 @@ to implement caching, filters (like syntax highlighting), etc. You can use `this.name` to access the attribute `name` from your view. -### Dereferencing Section +### Dereferencing Sections -If you have a nested object structure in your view, it can sometimes be easier -to use sections like this: +If your data has components that are logically grouped into nested objects, +you may wish to dereference an object to access its values. - var objects = { - a_object: { - title: 'this is an object', - description: 'one of its attributes is a list', - a_list: [{label: 'listitem1'}, {label: 'listitem2'}] +Given this object: + + { + "name": "Bill", + "address": { + "street": "801 Streetly street", + "city": "Boston", + "state": "MA", + "zip" "02101" } - }; + } + +And this template: + +

Contact: {{name}}

+ {{#address}} +

{{street}}

+

{{city}}, {{state}} {{zip}}

+ {{/address}} + +We'll get this output: -This is our template: - - {{#a_object}} -

{{title}}

-

{{description}}

- - {{/a_object}} - -Here is the result: - -

this is an object

-

one of its attributes is a list

- +

Contact: Bill

+

801 Streetly street

+

Boston, MA 02101

### Inverted Sections From 68ad470561d392f98ed6dd3549b587cb59d7a382 Mon Sep 17 00:00:00 2001 From: Tim Harper Date: Wed, 9 Nov 2011 00:40:36 -0700 Subject: [PATCH 2/2] README: Adds a note about dot notation --- README.md | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 7b134b2..0ded2cd 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,21 @@ Tags are always surrounded by mustaches like this `{{foobar}}`. template = "{{say_hello}}, {{name}}" +#### Accessing values in nested objects (Dot Notation) + +To access data logically grouped into nested objects, specify a '.' delimited +path to the value. + + var contact = { + name: {first: "Bill", last: "Bobitybob" }, + age: 37 + } + + template = "Hello, {{name.first}} {{name.last}}. You are {{age}} years old." + +*NOTICE*: The dot notation feature was recently implemented for the 0.4 + release, which is not out as of Nov 9 2011. You can find the feature in the + current master branch of mustachejs. ### Conditional Sections @@ -252,16 +267,6 @@ own iteration marker: {{bob}} {{/foo}} -## F.A.Q. - -### Why doesn’t Mustache allow dot notation like `{{variable.member}}`? - -The reason is given in the [mustache.rb -bugtracker](http://github.com/defunkt/mustache/issues/issue/6). - -Mustache implementations strive to be template-compatible. - - ## More Examples and Documentation See `examples/` for more goodies and read the [original mustache docs][m]