I was in need of checking if a macro parameter was empty. The following is a extremely simplified version of the actual code:
\define isStringEmpty(str)
<$set name="answer" filter="""$str$""" value="no" emptyValue="yes">
<<answer>>
</$set>
\end
The result was not as I expected:
Macro call | Result |
<<isStringEmpty "something">> | no |
<<isStringEmpty "">> | no |
After a lot of attempts I realized that this one: <$set name="answer" filter="" value="no" emptyValue="yes">
... behaves just like: <$set name="answer" value="no">
I mean it seems that having the filter parameter of the SetWidget set to an empty string, is just like not having it at all, therefore the emptyValue parameter is ignored and it falls back to the basic syntax (name, value).
It is probably obvious for the one more experienced with Tiddlywiki and HTML, I think it's a consequence of the way the widgets are parsed (as I could understand, quite like normal HTML tags). For me and the ones used with traditional programming languages, a variable set to an empty string is... an empty value!
I found a workaround and I decided to share my experience for who should fall in the same trouble, since I spent a lot of time to understand what happend, why and how to resolve.
\define isStringEmpty(str)
<$set name="answer" filter="""[enlist[$str$]]""" value="no" emptyValue="yes">
<<answer>>
</$set>
\end
Passing the string as a parameter of enlist filter operator do the trick. [enlist[]]
evaluates to an empty list (it could be easily tested in AdvancedSearch), as expected by the SetWidget while using conditional assignment:
Macro call | Result |
<<isStringEmpty "something">> | no |
<<isStringEmpty "">> | yes |
Hope this could help someone.
\define test(arg)<$set name=answer filter=" $arg$" value="full" emptyValue="empty" >ans:<<answer>></$set>\end