Dynamically render partial templates using mustache

Posted by btakita on Stack Overflow See other posts from Stack Overflow or by btakita
Published on 2010-05-28T21:34:17Z Indexed on 2010/05/28 21:42 UTC
Read the original article Hit count: 229

Is there a way to dynamically inject partial templates (and have it work the same way in both Ruby & Javascript)? Basically, I'm trying to render different 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_message}}

  {{#is_picture}}
  {{< picture}}
  {{/is_picture}}

  {{^is_picture}}
  {{/is_picture}}

  {{/is_message}}
</div>
{{/items}}
</div>

For obvious reasons, I'm not super-psyched about this approach. Is there a better way?

Also note that the different types of models for the views can have non-similar fields. I suppose I could always go to the lowest common denominator and have the data hash contain the html, however I would rather use the mustache templates.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about ruby