Discussion:
[FreeMarker-user] Handling of optional data tree
tim
2015-02-11 15:49:04 UTC
Permalink
Hi,

I have a data model of this kind:

contract
+contractParties
+contractParty
++share
+++amount
+++currency
...

I want to display amount and share for each contract party:
<#list contract[0].contractParties.contractParty as p>
Amount: ${p.share.amount!}
Share: ${p.share.currency!}
</#list>

list creates a sequence and that's fine. If there is no contract party, this
will just be omitted -> fine.
But then I want to access amount anf currency of the share. This works if
there is a share. It does not work if there is no share, though. If share is
null/empty/missing, it creates an error no matter what I do. I tried
${p.share[0].currency!} and ${p.share[0]!.currency!}, but it does not work
if share is undefined.

Is it possible to either
- turn off error handling for missing data model fractions or
- ignore missing paths
?

Thanks for your feedback!



--
View this message in context: http://freemarker.624813.n4.nabble.com/Handling-of-optional-data-tree-tp4655366.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Darryl Booms
2015-02-11 19:08:35 UTC
Permalink
Tim,

Perhaps you can check for the model data's existance first?
<#if (p.share??)> ... </#if>

Darryl
Post by tim
Hi,
contract
+contractParties
+contractParty
++share
+++amount
+++currency
...
<#list contract[0].contractParties.contractParty as p>
Amount: ${p.share.amount!}
Share: ${p.share.currency!}
</#list>
list creates a sequence and that's fine. If there is no contract party, this
will just be omitted -> fine.
But then I want to access amount anf currency of the share. This works if
there is a share. It does not work if there is no share, though. If share is
null/empty/missing, it creates an error no matter what I do. I tried
${p.share[0].currency!} and ${p.share[0]!.currency!}, but it does not work
if share is undefined.
Is it possible to either
- turn off error handling for missing data model fractions or
- ignore missing paths
?
Thanks for your feedback!
--
http://freemarker.624813.n4.nabble.com/Handling-of-optional-data-tree-tp4655366.html
Sent from the freemarker-user mailing list archive at Nabble.com.
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Daniel Dekany
2015-02-11 19:20:12 UTC
Permalink
The syntax for what you want is:


(p.share.currency)!

That is, by default ! only guards the last step, but with parenthesis
it guards the whole expression inside.

Also, you should update FreeMarker. 2.3.21 already explains this in
the error message.
--
Thanks,
Daniel Dekany
Post by tim
Hi,
contract
+contractParties
+contractParty
++share
+++amount
+++currency
...
<#list contract[0].contractParties.contractParty as p>
Amount: ${p.share.amount!}
Share: ${p.share.currency!}
</#list>
list creates a sequence and that's fine. If there is no contract party, this
will just be omitted -> fine.
But then I want to access amount anf currency of the share. This works if
there is a share. It does not work if there is no share, though. If share is
null/empty/missing, it creates an error no matter what I do. I tried
${p.share[0].currency!} and ${p.share[0]!.currency!}, but it does not work
if share is undefined.
Is it possible to either
- turn off error handling for missing data model fractions or
- ignore missing paths
?
Thanks for your feedback!
--
http://freemarker.624813.n4.nabble.com/Handling-of-optional-data-tree-tp4655366.html
Sent from the freemarker-user mailing list archive at Nabble.com.
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Loading...