Re: Global default expression/variable value policy

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
There's no setting that magically changes undefined variables to ""
(or who knows what the sensible default would be... maybe 0, or []
etc., depending on the place). Although you could do such a trick at
certain places with a special ObjectWrapper, that would change the
behavior of your templates where exp!default is used.

The closest thing to what you want is this:
http://freemarker.org/docs/pgui_config_errorhandling.html#autoid_44

Here, instead of substituting an undefined variable, you can
substitute a whole failing ${...} or <@.../> <@...>...</@...> or
<#...> or <#...>...</#...>.

In your case, you want to examine if the error is instanceof
freemarker.core.InvalidReferenceException, and only suppress the
exception then. In such case you could also issue some kind of special
log entry so these issues can be fixed by the maintainers.


Thursday, January 20, 2011, 9:40:10 AM, Ing. Jan Novotný wrote:

> Hello, 
>
>    I wonder whether there is a way how to set a default variable
> policy globally in FreeMarker. Consider these cases, when myVar has null value:
>
> 1. ${myVar!""} <#-- prints empty string -->
> 2. ${myVar} <#-- throws exception -->
>
>    This NULL treatment is very good for development and test
> environments, but for production I'd like the second example to not
> throw exception but instead print some default value (such as empty
> string). My experience is that developers (webdesigners in my case)
> despite all the testing efforts often "forget" to define default
> values even for non mandatory things. This finally leads to errors
> in production that could be many times avoided if I have chance to
> set a strategy how to behave when retrieving variable value fails due to not specified default.
>
>    Is there any extension point how can I plug in some custom logic for this use case?
>
> Thanks for any advices,
> Jan

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires 
February 28th, so secure your free ArcSight Logger TODAY! 
http://p.sf.net/sfu/arcsight-sfd2d
_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
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.