Re: Referencing Hash Values from Keys

"Shelley, Ryan" <[email protected]> Tue, 27 Oct 2009 17:15:48 +0000
Newsgroups gmane.comp.text.fmpp
Message-ID <CDD88F99FC3CE94CB8CAE5593D178A8C01DDCD12@SM-CALA-VXMB03A.swna.wdpr.disney.com>
Well, I think I'm just not familiar with FM yet, and I'm running this as
a Maven FMPP Ant Task, so what it seems was happening was that it was
pulling in a different config file than I thought, and that config file
didn't have a "poolConnections" value in one of the "pools", and since
FM isn't as lenient with missing keys it was throwing an error.  Looks
like I need to add some existence checking prior to accessing the hash
value.  Sorry for the false alarm.  I'm going to keep at it, but I think
it was just user-error.  Thanks for checking!

-Ryan

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Daniel
Dekany
Sent: Tuesday, October 27, 2009 1:48 AM
To: FMPP open discussion
Subject: Re: [FMPP] Referencing Hash Values from Keys

Tuesday, October 27, 2009, 5:45:11 AM, Shelley, Ryan wrote:

> I'm new to Freemarker, but have used other templating languages in
> the past.  Currently, I'm attempting to use FMPP to generate
> configuration files, but having a bit of difficulty referencing hash
> values from keys.  Here is my TDD and sample template:
>  
> TDD:
>  
> {
>       pools: [
>             {
>                   poolName: Foo
>                   poolConnections: ServiceA
>             },
>             {
>                   poolName: Bar
>                   poolConnections: ServiceB
>             }
>       ]
> }
>  
> Template:
>  
> This will output fine:
> <#list pools as pool>
>       <#assign keys = pool?keys >
>       <#list keys as key>
>             ${key} = ${pool[key]}
>       </#list>
> </#list>
>  
> This will get an error on poolConnections:
> <#list pools as pool>
>       ${pool.poolName}
>       ${pool.poolConnections}
> </#list>
>  
> In the first list iteration, everything is output correctly, but I
> have no control over the keys.  However, the second iteration fails
> on "${pool.poolConnections}" with the error:
>  
> Caused by: freemarker.core.InvalidReferenceException: Expression
> pool.poolConnections is undefined (notice the error is not on
pool.poolName)
>
> If I take "${pool.poolConnections}" out, it works fine (I see the
> value for poolName), but I kind of need to be able to access
"poolConnections"!
>  
> I've tried this with different combinations of

Don't... it should work the way you tried to do it:
pool.poolConnections

I copy-pasted your above snippets into an FMPP project and it works
fine here. Even if you use different versions than me, I don't know
about any bugs in FreeMarker or FMPP bugs that would spoil a such
basic operation. Are you absolutely sure you don't have a typo
somewhere, and that you look at the files that your project actually
uses?

> "${pool[poolConnections]}" and "${pool['poolConnections']}" and
> "${pool.get(poolConnections)}" but none seem to be correct.  I've
> been back and forth through the documentation and the examples, but
> none seem to cover accessing a hash value by it's key directly.

FreeMarker documentation does. It's myMap.myKey or
myMap[WhateverExpressionThatEvaluatesToAString] (like myMap["myKey"]
or myMap[aVariable], myMap["foo_" + i], etc.).

> Suggestions?  Thanks!
>  
> -Ryan

-- 
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
_______________________________________________
fmpp-open mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/fmpp-open

------------------------------------------------------------------------------
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