Global default expression/variable value policy

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Global default expression/variable value policy

Ing. Jan Novotný
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

------------------------------------------------------------------------------
Protect Your Site and Customers from Malware Attacks
Learn about various malware tactics and how to avoid them. Understand
malware threats, the impact they can have on your business, and how you
can protect your company and customers by using code signing.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Global default expression/variable value policy

Daniel Dekany
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Global default expression/variable value policy

Ing. Jan Novotný

Thank you very much. This is exactly that I have been looking for. Global default variables is obviously nonsense when there is possibility to handle processing errors by this way. I need only to make FreeMarker continue rendering even in case of such error in production (of course with loging appropriate error). Thank you again.

Cheers, Jan

Dne 21.1.2011 8:30 "Daniel Dekany" <[hidden email]> napsal(a):
> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user