Re: Using Variable as a List name

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Thursday, October 8, 2009, 6:50:14 AM, rizzz86 wrote:

> What I want to say is that, "argList1" contains the menu names:
>
> List argList1 = new ArrayList();
> argList1.add(menu1);
> argList1.add(menu2);

So, menu1 and menu2 are String-s.

> And "argList2, argList3" contains the subMenus of "menu1, menu2"
> respectively. (This will be done while populating "argList1") It will look
> like as follows:
>
> // subMenus of menu1
> List argList2 = new ArrayList();
> argList2.add(subMenu1);
> argList2.add(subMenu2);
> argList2.add(subMenu3);
>
> // subMenus of menu2
> List argList3 = new ArrayList();
> argList3.add(subMenu4);
> argList3.add(subMenu5);
> argList3.add(subMenu6);
>
> I am passing HashMap "root" as the argument for by freemarker template by
> setting "argList1, menu1, menu2" keys.
>
> HashMap root = new HashMap();
> root.put("argList1",argList1);
> root.put("menu1",argList2);
> root.put("menu2",argList3);

I assume argLits1 contains the list of strings: "menu1", "menu2"

> Now the .ftl have an outer loop for "argList1" (for main menus) and inner
> loop (for their subMenus). Another thing is that I doesn't know the the menu
> names (i.e. menu1, menu2) while rendering my subMenus.

Why not? It's in the loop variable of the outer loop, isn't it?

> I have to get these menu names from the "argList1" and use them as a
> list to access "root.put("menu1",argList2) or
> root.put("menu2",argList3)"
>
> Doing this <#foreach subMenu in .vars[menu]> gives the following Error
> "freemarker.core.InvalidReferenceException: Expression .vars[menu] is
> undefined"

Well, what's the value of the "menu" variable? If it's the String
"menu1" or "menu2", and you have such top-level variables (as you put
the into the root above), it must work.

> OR can someone suggest a better way to render these subMenus.

If there is a problem here, then is not with the rendering, but with
the odd data structure you are using. Since the menu system is
hierarchical, the data-structure should be too. I would create a
MenuItem class that is a JavaBean, and has a "title" and "submenus"
property, last being a List<MenuItem>. Then I don't have to type the
titles twice just to associate menu items with submenus (and thus
potential making a mistake), traversal would be trivial, and I can
easily associated other properties to MenuItems later (like icon,
action, etc.).

> I also cannot able to use 2D Arrays in ftl's.

Why not? It should work.

> rizzz86
>
>
>
>
> rizzz86 wrote:
>> 
>> Hi all,
>> 
>> I am trying to use any variable as a name of list ... i.e.,
>> 
>> <#foreach list1 in argList1>
>>      <#foreach list2 in list1> 
>>                      // The name of list2 is the first element of list1
>>      </#foreach>
>> </#foreach>
>> 
>> Following error occured if I pass variable as a list name "Expected
>> collection or sequence. list1 evaluated instead to
>> freemarker.ext.beans.StringModel"
>> 
>> Can anyone help ?
>> 
>> rizzz86

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.