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

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Albert Kam
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 ?

Thank you ..

--
Do not pursue the past. Do not lose yourself in the future.
The past no longer is. The future has not yet come.
Looking deeply at life as it is in the very here and now,
the practitioner dwells in stability and freedom.
(Thich Nhat Hanh)

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

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

Daniel Dekany
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
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user