How to do a grails while loop a certain number of times -


how can kick while loop n times in grails/groovy in side gsp? current approach:

%{-- <%= "it's groovy, man".take(88)    %> --}%      <g:set var="i" value="${messages.size()}"/>     <g:set var="x" value="<%= - 3 %>/>"     <g:while test="${i > x}">     <g:set var="i" value="${i-1}"/>       ... fancy html here       <%= messages[i].name %>     </g:while> 

solution:

  <g:set var="i" value="${messages.size()}"/>     <g:set var="k" value="${i-3}"/>     <g:while test="${i > k}"> 

so here do. assuming want last x items in list, think asking, although isn't clear. assuming messages list.

<g:set var="length" value="${messages.size()}" /> <g:each in="${messages.getat((math.min(length,3)*-1)..-1)}" var="message"> ... fancy html here       <%= message.name %> </g:each> 

here tests did confirm this:

def list = [1,2] def x = list.size() assert [1,2] == list.getat((math.min(x,3)*-1)..-1) list = [1,2,3] x = list.size() assert [1,2,3] == list.getat((math.min(x,3)*-1)..-1) list = [1,2,3,4] x = list.size() assert [2,3,4] == list.getat((math.min(x,3)*-1)..-1) 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -