Let's say I have a collection that is setup in my controller, named "things." In this collection is a heterogeneous assortment of objects. Let's say some are of type "Thing" and some others are of type "OtherThing". In the GSP page that renders this list, I want to render each different object type differently.
I know that the following code works, but I can't escape the feeling that there should be a more elegant way to do this. Does anybody have a better way?
<div id="eventStream">
<ul>
<g:each in="${things}" var="thing">
<g:if test="${thing.type.equals( 'Thing' )}">
<g:render template="/renderThing" bean="thing" />
</g:if>
<g:if test="${thing.type.equals( 'OtherThing')}">
<g:render template="/renderOtherThing" bean="thing" />
</g:if>
</g:each>
</ul>
</div>
(Note: there is a "type" field defined on 'Thing' and 'OtherThing' that contains those string literals. That's just a shortcut).
EDIT: Just realized that there's another way to do this, that depends on the notion of "convention over configuration." I could just go with a convention that the templates are always named something like:
_renderXXXXTemplate.gsp where XXX is "Thing" or "OtherThing" or whatever. Then I just render with something like:
<g:render template="/render${thing.class.name}Template" bean="thing" />
Anybody care to share their thoughts on the merit of that approach?