velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andy Lee <ag...@earthlink.net>
Subject RE: discarding method return values
Date Mon, 04 Nov 2002 18:37:06 GMT
At 11:57 AM -0500 11/4/02, Kevin Baynes wrote:
>  > #set( $myLongWords = [] )
>  > $myLongWords.add("rhinoceros")
>  > $myLongWords.add("callipygian")
>  > $myLongWords.add("lactovegetarian")
>  > $myLongWords.add("dodecahedron")
>>  $myLongWords.add("subliminable")    ## political humor
>>  $myLongWords.add("Schwarzenegger")
>>  $myLongWords.add("0xDEADBEEF")      ## geek humor
>
>Not to be contentious, merely friendly discussion here. :-)

Yup, no worries.

>  Once you are in the Template, what is the difference between 
>writing the above list and calling #foreach, or simply writing:
><select name="myWordRoots">
>  <option value="Rhinocerotidae">Rhinoceros
>  <option value="kallipugos">callipygian
>  ....
></select>

I interpret this question as: "Why use an array at all?"  You guessed 
part of my answer below, which is that I might want to use the array 
in more than one place.  Or I might want to do something with the 
array index, like:


<select name="myWordRoots">
   #set( $i = 0 )
   #foreach( $word in $myLongWords )
     <option value="$i">$word</option>
     #set( $i = $i + 1 )
   #end
</select>


Another part of my answer has to do with personal preferences I have 
about coding.  One is that I like to keep the code for an HTML 
element small if possible.  In this case I have an opportunity to 
keep the distance between <select> and </select> quite small, 
regardless of the number of <option> elements in between.

Imagine if this were a <table> instead of a popup list, and the table 
has multiple columns and <font> tags and bits of stuff like...

     bgcolor="#if( $evenRow )#FFFFFF#else#DDDDDD#end"

...all over the place.  Looping through an array starts to look a lot 
more attractive, especially when a designer decides #DDDDDD should 
really be #CCCCCC.

>(Truly inquisitive tone follows)
>Why is it easier to reorder the method calls above than to reorder the HTML?

It's not, at least in the simple case of just <option> elements within a menu.

It *is* easier to reorder the method calls than to reorder items in a 
long array literal like


#set( $myLongWords = [ "rhinoceros", "callipygian", 
"lactovegetarian", "dodecahedron", "subliminable", "Schwarzenegger", 
"0xDEADBEEF" ] )


You don't have to worry about commas, and if you use CVS and someone 
does a diff, it's easier to see what changed than if you check in:


#set( $myLongWords = [ "rhinoceros", "callipygian", "dodecahedron", 
"lactovegetarian", "subliminable", "Schwarzenegger", "0xDEADBEEF" ] )


Plus, again, with the separate lines you can comment each item individually:


     $myLongWords.add("lactovegetarian")  ## except Sundays


>Why would you need to build the list using Java objects in the Template?

I wouldn't need to, but I might prefer to, for the reasons above.  I 
think it makes my job easier and less error-prone.

>The only reason I can think of is if you repeated the same list a 
>number of times, having the same popup menu in numerous places... in 
>which case you could do:
>
><select>
>#parse("myLongWordOptions.vm")
></select>

I might want to reuse the array for other purposes.

Maybe in addition to the popup menu, I'm using the array to generate 
a table of contents, and I want to use the names both to generate 
hyperlinks and to create anchor links farther down the page:


#foreach( $word in $myLongWords )
   <a href="#$word">$word</a>
#end

## ...

#foreach( $word in $myLongWords )
   <hr>
   <h2><a name="$word">$word</a></h2>
   <p>
   $myLongWordDefinitions.get($word)
#end


--Andy

--
To unsubscribe, e-mail:   <mailto:velocity-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:velocity-user-help@jakarta.apache.org>


Mime
View raw message