From 38ca0cef689b8b82f9911d6ec9628b3ec7493500 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Mon, 26 Apr 2010 22:44:22 +0200 Subject: [PATCH] allow using sections as context. Patch by dpree. --- CHANGES.md | 2 ++ README.md | 34 ++++++++++++++++++++++++++++++++++ mustache.js | 3 +++ 3 files changed, 39 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index bd88ae9..ee9d66a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,9 +1,11 @@ # mustache.js Changes ## 0.3.0 (??-??-????) +* Use sections to dereference subcontexts. * Added higher order sections. + ## 0.2.3 (28-03-2010) * Better error message for missing partials. diff --git a/README.md b/README.md index 7c1e8e0..cfca7be 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,40 @@ implement caching, filters (like syntax highlighting), etc. You can use `this.name` to access the attribute `name` from your view. +### Dereferencing Section + +If you have a nested object structure in your view, it can sometimes be easier +to use sections like this: + + var objects = { + a_object: { + title: 'this is an object', + description: 'one of its attributes is a list', + a_list: [{label: 'listitem1'}, {label: 'listitem2'}] + } + }; + +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

+ + ### View Partials diff --git a/mustache.js b/mustache.js index 4a73e22..27fe6f2 100644 --- a/mustache.js +++ b/mustache.js @@ -109,6 +109,9 @@ var Mustache = function() { return that.render(content, that.merge(context, that.create_context(row)), partials, true); }).join(""); + } else if(that.is_object(value)) { // Object, Use it as subcontext! + return that.render(content, + that.merge(context, that.create_context(value)), partials, true); } else if(typeof value === "function") { // higher order section return value.call(context, content, function(text) {