2

Is there any way to pass a variable context to the {{render}} helper?

For instance, I have a polymorphic relationship on my model, and I want to render the appropriate view for each different type (without have to write a whole string of if statements.

my events template looks like this:

<ul>
{{#each event in model}}
    <li>
        {{event.time}} {{render event.type event.eventable}}
    </li>
{{/each}}
</ul>

the event.type is a string and is set to song. If I use {{render 'song' event.eventable}} everything works great. But passing the variable string 'song' produces nothing.

Can this be done?

4

1 回答 1

4

您可以通过编写自己的 Handlebars Helper 来实现这一点,该 Helper 确定要渲染的模板,然后委托给内置渲染助手。尝试这个:

Ember.Handlebars.registerBoundHelper('renderEvent', function(callingContext, event, options) {
  return Ember.Handlebars.helpers.render.call(callingContext, event.get('type'), 'eventable', options);
});

然后在您的模板中,您将按如下方式调用模板:

{{#each event in model}}
  {{renderEvent this event}}
((/each}}
于 2013-05-15T06:49:22.530 回答