janl/mustache.js — pull request #242 by thelucid (2012-08-06 23:11:47+00:00)
Title: Dynamic partials and partial collections

Hi,

This is a small patch that brings great flexibility when dealing with collections that are going to be rendered using partials. Please see the new documentation below:

* https://github.com/thelucid/mustache.js#dynamic-partials
* https://github.com/thelucid/mustache.js#partial-collections

With this patch, the following:

View:
```javascript
{
items: [
{ type: 'image', url: 'Some URL', is_image: true },
{ type: 'text', content: 'Some text', is_text: true }
]
}
```

Template:
```mustache
base.mustache
{{#items}}
{{#is_text}}
{{>text}}
{{/is_text}}
{{#is_image}}
{{>image}}
{{/is_image?}}
{{/items}}

text.mustache

{{content}}

image.mustache

```

...can be replaced with:

View:
```javascript
{
items: [
{ partial: 'text', content: 'Some text' },
{ partial: 'image', url: 'Some URL' }
]
}
```

Template:
```mustache
base.mustache
{{@items}}

text.mustache

{{content}}

image.mustache

```

...or if more fine grain control is needed:

Template:
```mustache
base.mustache
{{#items}}

{{>.}}

{{^items}}

text.mustache
{{content}}

image.mustache

```

Patch includes tests and documentation, addressing a recurring problem when using Mustache templates e.g. http://stackoverflow.com/questions/2932679/dynamically-render-partial-templates-using-mustache and https://github.com/janl/mustache.js/issues/241.

I really hope the pull request is accepted as I am already utilising this in a couple of projects and it's a joy to use. I will look at implementing in the Ruby version of Mustache also if accepted.

Kind regards,

Jamie