My revision code said, change the current one as it doesn’t follow the principle of "Don't repeat yourself" en.wikipedia.org/wiki/Don%27t_repeat_yourself The if else construct needs to be changed. Technology ".vm", java apach velocity template, but I think this is not very important.

#set($first = "true") #foreach($item in $data) #if($first == "true") <div class="tabs first" id="$item.get('Key')"> #set($first = "false") #else <div class="tabs" id="$item.get('Key')"> #end <h1>str1</h1> <h1>str2</h1> <h1>str3</h1> </div> ##//Close div created in (if else) #end 

    3 answers 3

     <div class="tabs #if($first == "true") first #set($first = "false") #end " id="$item.get('Key')"> 

    But, in my opinion, this is a typical example of sacrificing readability of a code to the dogmatic adherence to the “principle”.

      The sign of the first element in the search can be obtained from the $foreach variable : $foreach.first

      Therefore, the code can take the form:

       #foreach($item in $data) <div class="tabs #if($foreach.first) first#end" id="$item.get('Key')"> <h1>str1</h1> <h1>str2</h1> <h1>str3</h1> </div> ##//Close div created in (if else) #end 

        You can try to simplify to:

         #set($first = "first") #foreach($item in $data) <div class="tabs $first" id="$item.get('Key')"> ... </div> #if($first == "first") #set(first = "") #end #end 

        not familiar with this pattern language, correct. The idea should be clear