Dynamically create partial patterns with a mustache

Is there a way to dynamically introduce partial patterns (and work the same way in Ruby and Javascript)? Basically, I am trying to display various types of objects in a list.

The best I can come up with is this:

<div class="items"> {{#items}} <div class="item"> {{#is_message}} {{> message}} {{/is_message}} {{#is_picture}} {{> picture}} {{/is_picture}} </div> {{/items}} </div> 

I do not know much about this approach. Is there a better way?

Also note that different types of models for views may have different fields. I believe that I could always go to the lowest common denominator and have a data hash containing html, however I would prefer to use mustache patterns.

+6
javascript ruby templates partial-views
source share
1 answer

I did the same as you, and for each type of property I need a dynamic particle, I just set a dynamic variable in the js data model, which is displayed in the template ...

 eval("this.set({is_" + this.get("propertyType") + ": true})") 

or

 this["is_" + propertyType] = true 

At least I do not need to manually set the variable "is_whatever" ...

It would be great if mustache.js or ICanHaz.js had some kind of smart syntax for dynamic properties inside mustache tags ... maybe something like this:

 {{>{{message}} }} 
+1
source share

All Articles