Java

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • How to iterate the two different context in a single foreach loop

    5 answers - 875 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    hii
    I am new to velocity
    i am getting two context on the velocity template say
    context1 and context2. Both these contexts have an array of string and of same length.
    Now in a single loop i want to iterate the values of both the context. But i am not able to do that.
    what i was trying to do is that:-
    #set($counter = 0)
    #foreach($ctx1 in $context1)
    <select name="combobox">
    <option value="$context2[$counter]">$ctx1</option>
    </select>
    #end
    Here in the foreach loop $ctx1 is returning me the exact values but the $context2[$counter] is returning me the object of the array string.
    so i am not able to get the values. So is there any way in which i can iterate the two context in a single foreach loop or any another way you can suggest.
    Thanks in Advance
    Tarun
  • No.1 | | 1444 bytes | |

    Why r u using two different contexts ?
    Also this problem can be solved it we do context chaining

    Message
    From: "Tarun" <tarun.narang (AT) secf (DOT) com>
    To: "Velocity Users List" <velocity-user (AT) jakarta (DOT) apache.org>
    Sent: Wednesday, June 22, 2005 5:35 PM
    Subject: How to iterate the two different context in a single foreach loop

    hii
    I am new to velocity
    i am getting two context on the velocity template say
    context1 and context2. Both these contexts have an array of string and of
    same length.
    Now in a single loop i want to iterate the values of both the context. But i
    am not able to do that.
    what i was trying to do is that:-
    #set($counter = 0)

    #foreach($ctx1 in $context1)

    <select name="combobox">

    <option value="$context2[$counter]">$ctx1</option>

    </select>

    #end

    Here in the foreach loop $ctx1 is returning me the exact values but the
    $context2[$counter] is returning me the object of the array string.

    so i am not able to get the values. So is there any way in which i can
    iterate the two context in a single foreach loop or any another way you can
    suggest.

    Thanks in Advance

    Tarun

    To unsubscribe, e-mail: velocity-user-unsubscribe (AT) jakarta (DOT) apache.org
    For additional commands, e-mail: velocity-user-help (AT) jakarta (DOT) apache.org
  • No.2 | | 2389 bytes | |

    Thanks Avinash for replying
    but i am not using the velocity framework. i am using the turbine framework
    with velocity as a view template only. i have stucked in this problem . is
    there any method in which a single foreach loop i can iterate two contexts.

    like this #foreach($ctx1 in context1 , $ctx2 in context2) obviously this is
    not working. but like that is there any other way.

    Message
    From: "avanish" <avanish.awasthi (AT) secf (DOT) com>
    To: "Velocity Users List" <velocity-user (AT) jakarta (DOT) apache.org>
    Sent: Thursday, June 23, 2005 8:51 AM
    Subject: Re: How to iterate the two different context in a single foreach
    loop

    Why r u using two different contexts ?
    Also this problem can be solved it we do context chaining

    Message
    From: "Tarun" <tarun.narang (AT) secf (DOT) com>
    To: "Velocity Users List" <velocity-user (AT) jakarta (DOT) apache.org>
    Sent: Wednesday, June 22, 2005 5:35 PM
    Subject: How to iterate the two different context in a single foreach loop
    --
    hii
    I am new to velocity
    i am getting two context on the velocity template say
    context1 and context2. Both these contexts have an array of string and of
    same length.
    Now in a single loop i want to iterate the values of both the context. But
    i
    am not able to do that.
    what i was trying to do is that:-
    #set($counter = 0)

    #foreach($ctx1 in $context1)

    <select name="combobox">

    <option value="$context2[$counter]">$ctx1</option>

    </select>

    #end

    Here in the foreach loop $ctx1 is returning me the exact values but the
    $context2[$counter] is returning me the object of the array string.

    so i am not able to get the values. So is there any way in which i can
    iterate the two context in a single foreach loop or any another way you
    can
    suggest.

    Thanks in Advance

    Tarun
    >
    >
    >
    >
    >


    To unsubscribe, e-mail: velocity-user-unsubscribe (AT) jakarta (DOT) apache.org
    For additional commands, e-mail: velocity-user-help (AT) jakarta (DOT) apache.org
    --

    To unsubscribe, e-mail: velocity-user-unsubscribe (AT) jakarta (DOT) apache.org
    For additional commands, e-mail: velocity-user-help (AT) jakarta (DOT) apache.org
  • No.3 | | 4602 bytes | |

    Do you really mean two different contexts? just two different
    lists/collections/arrays, found in your context? Btw, I've always
    thought the ability to do something like:

    #foreach( $user in $users, $food in $foods )
    $user likes $food!
    #end

    would be incredibly convenient. Kinda wished Java 5 would have added
    this ability but instead we just got the same crippled foreach everyone
    else has. :-)

    Anyway, Tarun, you were close with your original workaround:

    <option value="$context2[$counter]">$ctx1</option>

    except that there's no syntax for array access, so it will just print
    out the full contents of your second list and then follow it with [#]
    each time. What you want to try instead is:

    <option value="$context2.get($counter)">$ctx1</option>

    make sure that $counter is one less than the $velocityCount
    automatic variable or you'll get an index out of bounds exception.

    Tarun wrote:

    >Thanks Avinash for replying
    >but i am not using the velocity framework. i am using the turbine framework
    >with velocity as a view template only. i have stucked in this problem . is
    >there any method in which a single foreach loop i can iterate two contexts.
    >
    >like this #foreach($ctx1 in context1 , $ctx2 in context2) obviously this is
    >not working. but like that is there any other way.
    >

    Message
    >From: "avanish" <avanish.awasthi (AT) secf (DOT) com>
    >To: "Velocity Users List" <velocity-user (AT) jakarta (DOT) apache.org>
    >Sent: Thursday, June 23, 2005 8:51 AM
    >Subject: Re: How to iterate the two different context in a single foreach
    >loop
    >
    >


    >
    >>Why r u using two different contexts ?
    >>Also this problem can be solved it we do context chaining
    >>

    >Message
    >>From: "Tarun" <tarun.narang (AT) secf (DOT) com>
    >>To: "Velocity Users List" <velocity-user (AT) jakarta (DOT) apache.org>
    >>Sent: Wednesday, June 22, 2005 5:35 PM
    >>Subject: How to iterate the two different context in a single foreach loop
    >>
    >>
    >>hii
    >>I am new to velocity
    >>i am getting two context on the velocity template say
    >>context1 and context2. Both these contexts have an array of string and of
    >>same length.
    >>Now in a single loop i want to iterate the values of both the context. But

    >
    >>

    >i


    >
    >>am not able to do that.
    >>what i was trying to do is that:-

    set($counter = 0)
    >>

    foreach($ctx1 in $context1)
    >>
    >><select name="combobox">
    >>
    >><option value="$context2[$counter]">$ctx1</option>
    >>
    >></select>
    >>

    end
    >>
    >>Here in the foreach loop $ctx1 is returning me the exact values but the
    >>$context2[$counter] is returning me the object of the array string.
    >>
    >>so i am not able to get the values. So is there any way in which i can
    >>iterate the two context in a single foreach loop or any another way you

    >
    >>

    >can


    >
    >>suggest.
    >>
    >>Thanks in Advance
    >>
    >>Tarun
    >>
    >>
    >>
    >>
    >>
    >>
    >>To unsubscribe, e-mail: velocity-user-unsubscribe (AT) jakarta (DOT) apache.org
    >>For additional commands, e-mail: velocity-user-help (AT) jakarta (DOT) apache.org
    >>
    >>

    >
    >>

    >
    >
    >
    >
    >
    >To unsubscribe, e-mail: velocity-user-unsubscribe (AT) jakarta (DOT) apache.org
    >For additional commands, e-mail: velocity-user-help (AT) jakarta (DOT) apache.org
    >


    To unsubscribe, e-mail: velocity-user-unsubscribe (AT) jakarta (DOT) apache.org
    For additional commands, e-mail: velocity-user-help (AT) jakarta (DOT) apache.org
  • No.4 | | 579 bytes | |

    Thursday, June 23, 2005, 7:43:19 AM, Jason Pettiss wrote:

    Do you really mean two different contexts? just two different
    lists/collections/arrays, found in your context? Btw, I've always
    thought the ability to do something like:

    #foreach( $user in $users, $food in $foods )
    $user likes $food!
    #end
    [snip]

    of curiosity, what are the real-world use-cases for this? Because if
    for users there is a food associated then probably there is a bean for
    each user with a food property, or a Map that maps user name to foods
    but two lists?
  • No.5 | | 2634 bytes | |

    The typical pattern is when you're augmenting one set of data with
    another.

    You could as you say create another bean which aggregates the two pieces
    of data that you need. But that's often not appropriate as you don't
    know until generation time what the data is. A common example is a
    table where the user gets to select which columns appear. You can
    easily get a query to get each of the columns individually, but it's
    much more difficult to generate a query on the fly which does it all in
    one shot.

    The most repeatedly annoying case that comes up is the problem Tarun
    has-- you're trying to generate an HTML SELECT item. Each PTIN has a
    value and a displayable label. You've likely already got a hardcoded
    list of allowable values, an enumeration. The labels typically come up
    as a list of strings from a ResourceBundle. Now you've got two lists.
    You should be done but you're not. Now you have to write or find
    some sort of LabelValueBean and then create N of these objects and throw
    in your two lists-- a big pain and a waste of resources.

    The summary gripe is-- if I've already got two or more lists of the same
    size and in the right order, why should I have to define custom objects
    whose only purpose is to aggregate, or worse-- throw one or more of the
    lists into one or more maps, and incur all the penalties of building and
    indexing a map (which I possibly only need for a single request).

    Daniel Dekany wrote:

    >Thursday, June 23, 2005, 7:43:19 AM, Jason Pettiss wrote:
    >


    >
    >>Do you really mean two different contexts? just two different
    >>lists/collections/arrays, found in your context? Btw, I've always
    >>thought the ability to do something like:
    >>

    foreach( $user in $users, $food in $foods )
    >>$user likes $food!

    end
    >
    >>

    >[snip]
    >

    of curiosity, what are the real-world use-cases for this? Because if
    >for users there is a food associated then probably there is a bean for
    >each user with a food property, or a Map that maps user name to foods
    >but two lists?
    >


    To unsubscribe, e-mail: velocity-user-unsubscribe (AT) jakarta (DOT) apache.org
    For additional commands, e-mail: velocity-user-help (AT) jakarta (DOT) apache.org

Re: How to iterate the two different context in a single foreach loop


max 4000 letters.
Your nickname that display:
In order to stop the spam: 0 + 9 =
QUESTION ON "Java"

EMSDN.COM