vector
vector

Reputation: 7576

Checking if object is of certain type in GSP

I have a list of domain objects in GSP view and would like to check if any of them are of particular type:

Class Equipment {}
Class Loader extends Equipment {}

... in view:

<g:each in="${Equipment.list()}" var="e">
   ... check if e is a Loader....
</g:each>

I'm trying to do the check if a GSP fragment to build a nav menu and wonder if this even the right spot to do the check in.

Upvotes: 2

Views: 1739

Answers (2)

Ernesto Campohermoso
Ernesto Campohermoso

Reputation: 7371

You can try:

<g:each in="${Equipment.list()}" var="e">
     <g:if test="${e instanceof your.package.Loader}">Do anything</g:if>
</g:each>

Upvotes: 5

Burt Beckwith
Burt Beckwith

Reputation: 75671

If you're making the logic in a GSP complex like that you should consider creating a taglib instead. It'll be easy to test too - GSPs need to be tested with functional tests and a running web server, but you can test taglibs with integration tests.

Upvotes: 9

Related Questions