You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Frans Thamura <ja...@intercitra.com> on 2003/01/30 05:49:22 UTC

[HELP] for website

Hi All, I need all you help.

My website, www.intercitra.com, blueoxygen.org, guerilla-forum.org is powered by Velocity

But i have a  difficulty to show the context inside loop, but using manual.

The case like this

I have 6 content in the context, want to be shown in the pages, and i usualy use foreach,

but in database there is a ResultSet syntax, such as myResult.next(), so i can use it manually, so i can put any context value any where. not in the loop arena.

so, where i can get it in Velocity. a next statement of context.

I need this help, 

Thnks



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


Re: [HELP] for website

Posted by Frans Thamura <ja...@intercitra.com>.
Oh, hi Andy, yah you get the point,

So, we must change the code and put the ResultSet into the Context, isnt it? Is there another alternatif to make the iterator works as a ResultSet in Velocity, such as change the #stop and make it continue again in another foreach loop.

I will try. you tips. wait my reply.


Thnks anyway, 



Frans




At 12:47 AM 1/30/2003 -0500, you wrote:
>At 11:49 AM +0700 1/30/03, Frans Thamura wrote:
>>but in database there is a ResultSet syntax, such as myResult.next(), so i can use it manually, so i can put any context value any where. not in the loop arena.
>
>If I understand correctly, you want to do something like
>
>
>    while (myResult.next())
>    {
>        x = myResult.getObject("column_one");
>        y = myResult.getObject("column_two");
>
>        // ... Display x and y ...
>    }
>
>
>And the problem is, there is no "while" in the Velocity Template Language.
>
>One thing you could do to simulate a while-loop is create a trivial subclass of Iterator that has a setFinishedFlag(boolean) method. Then you could use the fact that #foreach can take an Iterator.
>
>Suppose you had an instance of this Iterator in your context, under the name $myIterator.  Then something like this might work:
>
>
>    #set( $myIterator.FinishedFlag = $myResult.next() )
>
>    #foreach( $ignore in $myIterator )
>        #set( $x = $myResult.getObject("column_one") )
>        #set( $y = $myResult.getObject("column_two") )
>
>        ## ... Display x and y ...
>
>        ## Prepare for next loop iteration.
>        #set( $myIterator.FinishedFlag = myResult.next() )
>    #end
>
>
>Check out developer-guide.html in the Velocity docs, in particular the section "Support for Iterative Objects for #foreach()."  It explains why using Iterators can be tricky, but also says "There are good reasons to use the java.util.Iterator interface directly (large data sets via JDBC, for example)..."
>
>If you know your dataset is going to be *small*, you could skip all the above and simply add a utility method to some object in your context that puts the contents of the ResultSet into a List.  Then you could apply #foreach to that List.
>
>--Andy
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>



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


Re: [HELP] for website

Posted by Andy Lee <ag...@earthlink.net>.
At 11:49 AM +0700 1/30/03, Frans Thamura wrote:
>but in database there is a ResultSet syntax, such as 
>myResult.next(), so i can use it manually, so i can put any context 
>value any where. not in the loop arena.

If I understand correctly, you want to do something like


     while (myResult.next())
     {
         x = myResult.getObject("column_one");
         y = myResult.getObject("column_two");

         // ... Display x and y ...
     }


And the problem is, there is no "while" in the Velocity Template Language.

One thing you could do to simulate a while-loop is create a trivial 
subclass of Iterator that has a setFinishedFlag(boolean) method. 
Then you could use the fact that #foreach can take an Iterator.

Suppose you had an instance of this Iterator in your context, under 
the name $myIterator.  Then something like this might work:


     #set( $myIterator.FinishedFlag = $myResult.next() )

     #foreach( $ignore in $myIterator )
         #set( $x = $myResult.getObject("column_one") )
         #set( $y = $myResult.getObject("column_two") )

         ## ... Display x and y ...

         ## Prepare for next loop iteration.
    	#set( $myIterator.FinishedFlag = myResult.next() )
     #end


Check out developer-guide.html in the Velocity docs, in particular 
the section "Support for Iterative Objects for #foreach()."  It 
explains why using Iterators can be tricky, but also says "There are 
good reasons to use the java.util.Iterator interface directly (large 
data sets via JDBC, for example)..."

If you know your dataset is going to be *small*, you could skip all 
the above and simply add a utility method to some object in your 
context that puts the contents of the ResultSet into a List.  Then 
you could apply #foreach to that List.

--Andy

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