Foros de discusión

[Velocity] Foreach

Adrien G, modificado hace 13 años.

[Velocity] Foreach

New Member Mensajes: 14 Fecha de incorporación: 15/10/10 Mensajes recientes
Hi everyone, emoticon

Something weird is happening when I use foreach loop. emoticon

I define a map in init_custom.vm like this :

#set ($headerLinks = {"Item1" : "http://www.google.com",
						"Item2" : "http://www.google.com", 
						"Item3" : "http://www.google.com",
						"Item4" : "http://www.google.com", 
						"Item5" : "http://www.google.com", 
						"Item6" : "http://www.google.com", 
						"Item7" : "http://www.google.com"})


And then, in portal_normal.vm I do this :

		<div id="bannerTop">	
			#foreach( $key in $headerLinks.keySet() )
			    <a href="$headerLinks.get($key)">$key</a>
			#end
		</div>


And in my web page I get : emoticon

 Item1 Item2 Item7  Item4 Item3 Item6  Item5 


I don't understand why items are not display in the same order... the result is the same when I change the order of items in the map. emoticon

So, how it works ? Thanks ! emoticon
thumbnail
charles de courval, modificado hace 13 años.

RE: [Velocity] Foreach

Junior Member Mensajes: 55 Fecha de incorporación: 31/07/10 Mensajes recientes
Not so sure about this, but sets do not garantie the ordering. Try going for a list of some sort.
Adrien G, modificado hace 13 años.

RE: [Velocity] Foreach

New Member Mensajes: 14 Fecha de incorporación: 15/10/10 Mensajes recientes
Hi, thanks for your reply !

I tried to use entrySet() instead of keySet but it's the same issue. I don't find another solutions to browse a MapList.


            #foreach( $entry in $headerLinks.entrySet() )
                <a href="$entry.value">$entry.key</a>
                #if ($velocityCount &lt; $headerLinks.size())
                    <span>    |    </span>
                #end
            #end


Try going for a list of some sort

Could you be more precise ?

Thanks !
thumbnail
charles de courval, modificado hace 13 años.

RE: [Velocity] Foreach

Junior Member Mensajes: 55 Fecha de incorporación: 31/07/10 Mensajes recientes
What I wanted to say is to use some sort of list. The Map implementation does not garantie the order in which the elements were inserted in the Map. There are some exception to this, but I don't think that Velocity uses one of these classes.
wolfgang kubens, modificado hace 13 años.

RE: [Velocity] Foreach

New Member Mensajes: 12 Fecha de incorporación: 15/07/09 Mensajes recientes
look at this sample
#set ($menuItems= [])
#set ($tmp = $menuItems.add({"title" : "item1", "url" : "http://www.google.com"}))
#set ($tmp = $menuItems.add({"title" : "item3", "url" : "http://www.google.com"}))
#set ($tmp = $menuItems.add({"title" : "item2", "url" : "http://www.google.com"}))
#set ($tmp = $menuItems.add({"title" : "item4", "url" : "http://www.google.com"}))

#foreach ($item in $sortTool.sort($menuItems, 'title:asc'))
    <a href="$item.url">$item.title</a><br>
#end

#foreach ($item in $sortTool.sort($menuItems, 'title:desc'))
    <a href="$item.url">$item.title</a><br>
#end
Adrien G, modificado hace 13 años.

RE: [Velocity] Foreach

New Member Mensajes: 14 Fecha de incorporación: 15/10/10 Mensajes recientes
Thank you wolfgang, it works perfectly ;)

By the way, is there somewhere de doc reference of Velocity where I can find all objects / methods etc ? Because the user book is very restricted, for example I didn't know the existence of $sortTool function.

Have a nice day, bye !