Discussion:
[FreeMarker-user] How to check whether it's an empty string macro param ?
Albert Kam
2013-07-30 09:51:37 UTC
Permalink
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)
Daniel Dekany
2013-07-30 17:23:37 UTC
Permalink
Post by Albert Kam
<#macro test param>
</#macro>
But i want the param to be optional, so, i want to put a default
<#macro test param="">
<#if notEmptyString(param)>
....
<#else>
....
</#if>
</#macro>
<#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...
Post by Albert Kam
Thank you ..
--
Thanks,
Daniel Dekany
Loading...