Break in g:if / g:each

467 Views Asked by At

I have loop in gsp like this:

<g:each in="${personInstance.followed}" var="c" >
            <g:if test="${c.equals(person)}">
            <g:link id="${person.id}" action="unfollow" controller="message">unfollow</g:link>
            </g:if>
</g:each>

How can I use break in g:each or g:if? Any ideas?

2

There are 2 best solutions below

0
On

It sounds as if you are looking to display one thing or another based on if an instance is within a collection. Your best bet is to use contains on the collection. For example:

<g:if test="${personInstance.followed.contains(person)}">
  Display your unfollow stuff here ...
</g:if>
<g:else>
  Display your follow stuff here ...
</g:else>
0
On

Generally in Groovy and in GSP you don't need break and what you really want is to use the findAll tag which filters on a condition so your logic would become (untested example code):

<g:findAll in="${personInstance.followed}" expr="c.equals(person)" var="c" >
        <g:link id="${person.id}" action="unfollow" controller="message">unfollow</g:link>
</g:findAll>