Re: How to check whether it's an empty string macro param ?

Daniel Dekany <[email protected]> Tue, 30 Jul 2013 19:23:37 +0200
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Tuesday, July 30, 2013, 11:51:37 AM, Albert Kam wrote:

> I am currently using some macros that can accept a param, which can be string or sequence like this :
> <#macro test param>
> </#macro>
>
> But i want the param to be optional, so, i want to put a default
> value on it, and check whether the param is specified or not :
> <#macro test param="">
>     <#if notEmptyString(param)>
>         ....
>     <#else>
>         ....
>     </#if>
> </#macro>
>
> And here's my function on checking the emptiness of the param :
> <#function notEmptyString value>
>  <#return !(value?is_string) || value != "">
> </#function>
>
> Altough i havent faced any problems yet, i have my own doubts with
> this function, since the param can be of multiple types which will
> pass the ?is_string. If so, how do i do the checking correctly ?

I don't see any technical problems with that check. However, maybe
param?has_content is nicer (and much faster), assuming you can treat
"" and [] (and {}) the same.

I don't know about a trick to tell with 100% certainty if the
parameter was specified though. You could chose a value that's simply
unlikely enough to be passed in to be treated as the sign of that the
parameter was omitted. I know, ugly. We had to introduce a special
marker value that can only be used as the default of a parameter, like
`<#macro my param=.omitted><#if param?is_omitted>`, but there's no such
thing yet...

> Thank you ..

-- 
Thanks,
 Daniel Dekany


------------------------------------------------------------------------------
Get your SQL database under version control now!
Version control is standard for application code, but databases havent 
caught up. So what steps can you take to put your SQL databases under 
version control? Why should you start doing it? Read more to find out.
http://pubads.g.doubleclick.net/gampad/clk?id=49501711&iu=/4140/ostg.clktrk