Re: Dynamic interpolation names

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
In FreeMarker you can do both aHash.somethingFix and
aHash[somethingDynamic], where somethingDynamic can be whatever
expression, including a variable reference. The only tricky thing in
your case is to get a top-level hash somehow. For that see:
http://freemarker.org/docs/ref_specvar.html

So, for example you can write:

  <#if .data_model["menu" + menuNumber]??>
  
or if you want data-model shadowing to work:

  <#if .vars["menu" + menuNumber]??>

-- 
Best regards,
 Daniel Dekany


Friday, February 8, 2008, 4:26:31 PM, Andrew Moore wrote:

> Hi,
> I'm trying to display a list of data to the screen where each of the
> elements has a unique number
> for example i've done: 
>    root.put('menu1','details here');
>    root.put('menu2','details here');
>    root.put('menu3','details here');
>
> When it comes to displaying details in the freemarker template, I'm then
> writing out the data with something like this:
>       <#if menu1??> DO LOTS HERE</#if>
>       <#if menu2??> DO LOTS HERE</#if>
>       <#if menu3??> DO LOTS HERE</#if>
>
> I know it's not very nice because there's lots of repeating code, so I'd
> though of doing it as a macro.
> What I'd like is something like
>    <@listMenu menuNumber="1">
>    <@listMenu menuNumber="2">
>    <@listMenu menuNumber="3">
>
>     <#macro listMenu menuNumber>
>           <#if menu!MENUNUMBER!??> DO LOTS HERE</#if>
>     </#macro>
>
> where !MENUNUMBER! is the value passed into the parameter.
>
> But I'm not sure what syntax I should use. I've done macros where values
> have been passed in and used within the macro, but not where they form part
> of the name of the interpolation. Is there any way I can dynamically
> reference interpolation names?
>
> Hope that makes some sense!
>
> If there's a better way to do it also, that would be great, but due to the
> design of the system (that's not under my control) I'm stuck with menu1,
> menu2, menu3 etc instead of having a list.
>
> Thanks
> Andrew 



-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
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.