Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GSP: check if model (variable) is empty not working

Im new to grails (1.3.7) and Im trying to get something to work:

In my controller, I give back a few lists which I want to access in my gsp. Accessing works, but I only want to access them if they are not empty. The check if a list is empty or not does not work.

Here is what my controller gives back:

return new ModelAndView("/questions/questions", [ questionsList101 : allQuestions101, questionsList102 : allQuestions102, ... ])

allQuestions-objects are "def allQuestions.." containing Questions-Objects (Database-Object)

on my gsp now I try the following:

<g:if test="${!empty questionsList101}">  101:<br/>
<g:each in="${questionsList101}" var="elem" status="i">
  <g:checkBox name="${questionsList101[i].id}" value="${questionsList101[i].id}"/>${questionsList101[i].id}<br/>
</g:each>
<br/>
</g:if>

the loop is working, the check for emptiness is not. I tried with "not empty", "!empty", ... dont know whats wrong! any help is apreciated! :-)

like image 928
nano7 Avatar asked Jun 23 '11 14:06

nano7


1 Answers

The "grooviest" way to do this is

<g:if test="${questionList101}">

In Groovy, null objects and empty Collections are coerced to false. See the documentation on Groovy truth here: http://groovy-lang.org/semantics.html#Groovy-Truth

like image 132
Matt Lachman Avatar answered Sep 24 '22 13:09

Matt Lachman