You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Henning P. Schmiedehausen" <hp...@intermeta.de> on 2001/03/29 19:06:01 UTC
How to access an array [Possibly a really stupid question]
Hi,
I've played with Velocity a little and now I want to use it in a small
application. Basically this application creates a context with a
callback method and a row counter. The template should create a table
from it. The table should have (as this is considered "relevant" by
the UI designers) alternating colors for its rows.
I had the following pseudocode in mind:
--- cut ---
#set ( $color = [ "color1", "color2" ] )
#set ( $colsel = 0 )
<TABLE>
#foreach ($row in $tbl.getRows())
<TR BGCOLOR="$color[$colsel]">
<TD>$tbl.getData($row)</TD>
</TR>
#set ($colsel = 1 - $colsel)
#end
</TABLE>
--- cut ---
... and then I noticed that there seems to be no way to access an
array element by index. =:-( The only way that I found to access an
array element is with foreach.
So I had to resort to the following ugly beast:
--- cut ---
#macro( bgcol $colsel $c1 $c2 )
#if ($colsel == 0)
BGCOLOR="$c1"
#else
BGCOLOR="$c2"
#end
#end
#set ($colsel = 1)
#set ($c1="color1")
#set ($c2="color2")
#foreach ( $row in $tbl.getRows() )
<TR #bgcol($colsel $c1 $c2)>
<TD>$tbl.getData($row)</TD>
</TR>
#set ($colsel = 1 - $colsel)
#end
</TABLE>
--- cut ---
Ugh. Horrors. Did I miss something? How can I access a certain element
in an object array by index?
Regards
Henning
--
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen -- Geschaeftsfuehrer
INTERMETA - Gesellschaft fuer Mehrwertdienste mbH hps@intermeta.de
Am Schwabachgrund 22 Fon.: 09131 / 50654-0 info@intermeta.de
D-91054 Buckenhof Fax.: 09131 / 50654-20
Re: How to access an array [Possibly a really stupid question]
Posted by Jason van Zyl <jv...@apache.org>.
"Henning P. Schmiedehausen" wrote:
>
> Hi,
>
> I've played with Velocity a little and now I want to use it in a small
> application. Basically this application creates a context with a
> callback method and a row counter. The template should create a table
> from it. The table should have (as this is considered "relevant" by
> the UI designers) alternating colors for its rows.
>
> I had the following pseudocode in mind:
>
> --- cut ---
> #set ( $color = [ "color1", "color2" ] )
> #set ( $colsel = 0 )
>
> <TABLE>
> #foreach ($row in $tbl.getRows())
> <TR BGCOLOR="$color[$colsel]">
> <TD>$tbl.getData($row)</TD>
> </TR>
> #set ($colsel = 1 - $colsel)
> #end
> </TABLE>
Fortunately for you the [..] operator makes an ArrayList
and not an array :-) I haven't tested this but you can
probably use this:
<TABLE>
#foreach ($row in $tbl.getRows())
<TR BGCOLOR="$color.get($colsel)">
<TD>$tbl.getData($row)</TD>
</TR>
#set ($colsel = 1 - $colsel)
#end
</TABLE>
> --- cut ---
>
> ... and then I noticed that there seems to be no way to access an
> array element by index. =:-( The only way that I found to access an
> array element is with foreach.
>
> So I had to resort to the following ugly beast:
>
> --- cut ---
> #macro( bgcol $colsel $c1 $c2 )
> #if ($colsel == 0)
> BGCOLOR="$c1"
> #else
> BGCOLOR="$c2"
> #end
> #end
>
> #set ($colsel = 1)
> #set ($c1="color1")
> #set ($c2="color2")
>
> #foreach ( $row in $tbl.getRows() )
> <TR #bgcol($colsel $c1 $c2)>
> <TD>$tbl.getData($row)</TD>
> </TR>
> #set ($colsel = 1 - $colsel)
> #end
> </TABLE>
> --- cut ---
>
> Ugh. Horrors. Did I miss something? How can I access a certain element
> in an object array by index?
>
> Regards
> Henning
>
> --
> Dipl.-Inf. (Univ.) Henning P. Schmiedehausen -- Geschaeftsfuehrer
> INTERMETA - Gesellschaft fuer Mehrwertdienste mbH hps@intermeta.de
>
> Am Schwabachgrund 22 Fon.: 09131 / 50654-0 info@intermeta.de
> D-91054 Buckenhof Fax.: 09131 / 50654-20
--
jvz.
Jason van Zyl
jvanzyl@apache.org
http://jakarta.apache.org/velocity
http://jakarta.apache.org/turbine
http://tambora.zenplex.org