You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Κώστας Στεργίου <cs...@aias.gr> on 2001/01/24 15:56:03 UTC
Accessing array members using indexes OR creating select box dynamically
Hello,
I am quite new to velocity and I have a rather simple question.
I want to reference an array item from within velocity. For example,
let's say:
String names[] = new String[5];
...<init_name>
How can I access the names[2] String from within a .vm? (e.g. $names[2])
The original problem I am trying to solve is that I want to produce dynamically
a select box within my html so I need to have an array of values and labels
for this select box. For example:
I have a String array named values and another named labels into
the context object and within the vm template I try:
<SELECT NAME="example" >
#foreach ($val in $values)
<option value=$val>$labels[i]</option>
#end
</SELECT>
You see above that the #labels[i] is not correct. Maybe there is another
way to solve this problem that I am not aware of.
Any help would be greatly appreciated.
Thanks,
Costas
Re: Accessing array members using indexes OR creating select box dynamically
Posted by ??sta? Ste????? <cs...@aias.gr>.
Thanks very much,
Can you point me to this toolbox? Is it anywhere available?
----- Original Message -----
From: "Christoph Reck" <Ch...@dlr.de>
To: <ve...@jakarta.apache.org>
Sent: Wednesday, January 24, 2001 5:03 PM
Subject: Re: Accessing array members using indexes OR creating select box
dynamically
Put a toolbox with some static functions into the context.
The required method is:
/**
* Convenience function to access an element of an array.
*
* @param index
* @param array
* @return Element at the specified array index.
*/
public static Object getElement(int index, Object[] array)
{
return array[index];
}
/**
* Convenience function to access an element of an array and
* allow specifying a default value.
*
* @param index
* @param array
* @param dv The default value.
* @return Element at the specified array index.
*/
public static Object getElement(int index, Object[] array, Object dv)
{
Object value = (index >= array.length) ? dv : array[index];
return (value != null) ? value : dv;
}
Then you can do in your template:
<option value=$val>$toolbox.getElement(i, $labels)</option>
I submitted a set of utilites to the list some weeks ago, jvz mentioned
that they are putting a set togehter to be supplied wihtin velocity.
Hope this helped.
:) Christoph
> Êþóôáò Óôåñãßïõ wrote:
>
> Hello,
> I am quite new to velocity and I have a rather simple question.
> I want to reference an array item from within velocity. For example,
> let's say:
> String names[] = new String[5];
> ...<init_name>
>
> How can I access the names[2] String from within a .vm? (e.g. $names[2])
>
> The original problem I am trying to solve is that I want to produce
dynamically
> a select box within my html so I need to have an array of values and
labels
> for this select box. For example:
> I have a String array named values and another named labels into
> the context object and within the vm template I try:
>
>
> <SELECT NAME="example" >
> #foreach ($val in $values)
> <option value=$val>$labels[i]</option>
> #end
> </SELECT>
> You see above that the #labels[i] is not correct. Maybe there is another
> way to solve this problem that I am not aware of.
> Any help would be greatly appreciated.
> Thanks,
> Costas
Re: Accessing array members using indexes OR creating select box
dynamically
Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Or just throw the strings into a Vector in the first place, and then
$vector.elementAt(4)
...
geir
Christoph Reck wrote:
>
> Put a toolbox with some static functions into the context.
> The required method is:
> /**
> * Convenience function to access an element of an array.
> *
> * @param index
> * @param array
> * @return Element at the specified array index.
> */
> public static Object getElement(int index, Object[] array)
> {
> return array[index];
> }
>
> /**
> * Convenience function to access an element of an array and
> * allow specifying a default value.
> *
> * @param index
> * @param array
> * @param dv The default value.
> * @return Element at the specified array index.
> */
> public static Object getElement(int index, Object[] array, Object dv)
> {
> Object value = (index >= array.length) ? dv : array[index];
> return (value != null) ? value : dv;
> }
> Then you can do in your template:
> <option value=$val>$toolbox.getElement(i, $labels)</option>
>
> I submitted a set of utilites to the list some weeks ago, jvz mentioned
> that they are putting a set togehter to be supplied wihtin velocity.
>
> Hope this helped.
>
> :) Christoph
>
> > Êþóôáò Óôåñãßïõ wrote:
> >
> > Hello,
> > I am quite new to velocity and I have a rather simple question.
> > I want to reference an array item from within velocity. For example,
> > let's say:
> > String names[] = new String[5];
> > ...<init_name>
> >
> > How can I access the names[2] String from within a .vm? (e.g. $names[2])
> >
> > The original problem I am trying to solve is that I want to produce dynamically
> > a select box within my html so I need to have an array of values and labels
> > for this select box. For example:
> > I have a String array named values and another named labels into
> > the context object and within the vm template I try:
> >
> >
> > <SELECT NAME="example" >
> > #foreach ($val in $values)
> > <option value=$val>$labels[i]</option>
> > #end
> > </SELECT>
> > You see above that the #labels[i] is not correct. Maybe there is another
> > way to solve this problem that I am not aware of.
> > Any help would be greatly appreciated.
> > Thanks,
> > Costas
--
Geir Magnusson Jr. geirm@optonline.com
Velocity : it's not just a good idea. It should be the law.
http://jakarta.apache.org/velocity
Re: Accessing array members using indexes OR creating select box
dynamically
Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Keng Wong wrote:
>
> I'm new to Velocity. What is a toolbox ? Do I create my own classes and put
> it
> somewhere / extend from a base class ?
In general, 'toolbox' refers to a set of utility classes that are placed
into the context (VelocityContext, for example) and then accessed by the
template. An example of one is the
org.apache.velocity.util.tools.VelocityFormatter class.
geir
--
Geir Magnusson Jr. geirm@optonline.com
Velocity : it's not just a good idea. It should be the law.
http://jakarta.apache.org/velocity
RE: Accessing array members using indexes OR creating select box dynamically
Posted by Keng Wong <ke...@verizon.net>.
I'm new to Velocity. What is a toolbox ? Do I create my own classes and put
it
somewhere / extend from a base class ?
-----Original Message-----
From: Christoph.Reck@esrin.esa.it [mailto:Christoph.Reck@esrin.esa.it]On
Behalf Of Christoph Reck
Sent: Wednesday, January 24, 2001 7:04 AM
To: velocity-user@jakarta.apache.org
Subject: Re: Accessing array members using indexes OR creating select
box dynamically
Put a toolbox with some static functions into the context.
The required method is:
/**
* Convenience function to access an element of an array.
*
* @param index
* @param array
* @return Element at the specified array index.
*/
public static Object getElement(int index, Object[] array)
{
return array[index];
}
/**
* Convenience function to access an element of an array and
* allow specifying a default value.
*
* @param index
* @param array
* @param dv The default value.
* @return Element at the specified array index.
*/
public static Object getElement(int index, Object[] array, Object dv)
{
Object value = (index >= array.length) ? dv : array[index];
return (value != null) ? value : dv;
}
Then you can do in your template:
<option value=$val>$toolbox.getElement(i, $labels)</option>
I submitted a set of utilites to the list some weeks ago, jvz mentioned
that they are putting a set togehter to be supplied wihtin velocity.
Hope this helped.
:) Christoph
> Êþóôáò Óôåñãßïõ wrote:
>
> Hello,
> I am quite new to velocity and I have a rather simple question.
> I want to reference an array item from within velocity. For example,
> let's say:
> String names[] = new String[5];
> ...<init_name>
>
> How can I access the names[2] String from within a .vm? (e.g. $names[2])
>
> The original problem I am trying to solve is that I want to produce
dynamically
> a select box within my html so I need to have an array of values and
labels
> for this select box. For example:
> I have a String array named values and another named labels into
> the context object and within the vm template I try:
>
>
> <SELECT NAME="example" >
> #foreach ($val in $values)
> <option value=$val>$labels[i]</option>
> #end
> </SELECT>
> You see above that the #labels[i] is not correct. Maybe there is another
> way to solve this problem that I am not aware of.
> Any help would be greatly appreciated.
> Thanks,
> Costas
Re: Accessing array members using indexes OR creating select box
dynamically
Posted by Christoph Reck <Ch...@dlr.de>.
Put a toolbox with some static functions into the context.
The required method is:
/**
* Convenience function to access an element of an array.
*
* @param index
* @param array
* @return Element at the specified array index.
*/
public static Object getElement(int index, Object[] array)
{
return array[index];
}
/**
* Convenience function to access an element of an array and
* allow specifying a default value.
*
* @param index
* @param array
* @param dv The default value.
* @return Element at the specified array index.
*/
public static Object getElement(int index, Object[] array, Object dv)
{
Object value = (index >= array.length) ? dv : array[index];
return (value != null) ? value : dv;
}
Then you can do in your template:
<option value=$val>$toolbox.getElement(i, $labels)</option>
I submitted a set of utilites to the list some weeks ago, jvz mentioned
that they are putting a set togehter to be supplied wihtin velocity.
Hope this helped.
:) Christoph
> Êþóôáò Óôåñãßïõ wrote:
>
> Hello,
> I am quite new to velocity and I have a rather simple question.
> I want to reference an array item from within velocity. For example,
> let's say:
> String names[] = new String[5];
> ...<init_name>
>
> How can I access the names[2] String from within a .vm? (e.g. $names[2])
>
> The original problem I am trying to solve is that I want to produce dynamically
> a select box within my html so I need to have an array of values and labels
> for this select box. For example:
> I have a String array named values and another named labels into
> the context object and within the vm template I try:
>
>
> <SELECT NAME="example" >
> #foreach ($val in $values)
> <option value=$val>$labels[i]</option>
> #end
> </SELECT>
> You see above that the #labels[i] is not correct. Maybe there is another
> way to solve this problem that I am not aware of.
> Any help would be greatly appreciated.
> Thanks,
> Costas
Re: Accessing array members using indexes OR creating select box
dynamically
Posted by Christoph Reck <Ch...@dlr.de>.
The patch that Geir jsut submitted to CVS allows you now to access
array elements (defined within the template) via a method (change
from Object[] to ArrayList)
So if you did:
#set( $labels = ["a", "b", "c"] )
After you download the patch to
org/apache/velocity/runtime/parser/node/ASTObjectArray.java
from CVS, you can do:
<option value=$val>$labels.get(i)</option>
:) Christoph
> Hello,
> I am quite new to velocity and I have a rather simple question.
> I want to reference an array item from within velocity. For example,
> let's say:
> String names[] = new String[5];
> ...<init_name>
>
> How can I access the names[2] String from within a .vm? (e.g. $names[2])
>
> The original problem I am trying to solve is that I want to produce dynamically
> a select box within my html so I need to have an array of values and labels
> for this select box. For example:
> I have a String array named values and another named labels into
> the context object and within the vm template I try:
>
>
> <SELECT NAME="example" >
> #foreach ($val in $values)
> <option value=$val>$labels[i]</option>
> #end
> </SELECT>
> You see above that the #labels[i] is not correct. Maybe there is another
> way to solve this problem that I am not aware of.
> Any help would be greatly appreciated.
> Thanks,
> Costas