assign variable with default value / how to check if a variable has a value?

147 views
Skip to first unread message

Erwan

unread,
Oct 25, 2015, 10:46:49 AM10/25/15
to TiddlyWiki
Hi again everyone,

use case: I want to build MyTemplate which shows a link to the current
tiddler, with the text of the link being either a specific value given
as parameter, or "title" by default.

So I'm trying to assign a variable "v" with the value of variable
"param" if "param" is defined, or some default value otherwise. I tried
this:

<$set name="param" value="myvalue">
<$set name="v" filter="[<param>]" emptyValue="default">
<$text text=<<v>>>
</$set>
</$set>

but it doesn't work. I also tried various combinations like:

<$set name="v" filter="[<param>]" value=<<param>> emptyValue="default">

but that would assign either "myvalue" or nothing instead of
"default"... I'm confused.

I guess another way to phrase the question is: how can I check whether a
variable has a value or not?

Regards
Erwan



Mat

unread,
Oct 25, 2015, 4:34:52 PM10/25/15
to TiddlyWiki
Maybe the default values in macro call parameters could be used?

<:-)

FrD

unread,
Oct 25, 2015, 5:18:04 PM10/25/15
to TiddlyWiki
Hi Erwan,

I have a solution, but not a very elegant one :

\define myFilter()
a$(param)$ +[removesuffix[a]]
\end

<$set name="v" filter=<<myFilter>> value="default" emptyValue=<<param>>>
     <$text text=<<v>>/>
</$set>


<$set name="param" value="myValue">
<$set name="v" filter=<<myFilter>> value="default" emptyValue=<<param>>>
     <$text text=<<v>>/>
</$set></$set>

FrD

Erwan

unread,
Oct 25, 2015, 8:26:31 PM10/25/15
to tiddl...@googlegroups.com

Hi Mat,

I don't see anything about default values in the macrocall widget? can you be more specific?

Erwan


On 25/10/15 20:34, Mat wrote:
Maybe the default values in macro call parameters could be used?

<:-)
--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at http://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/5d43b17a-d2b9-44f3-9060-1e68fdc31753%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Erwan

unread,
Oct 25, 2015, 8:32:36 PM10/25/15
to tiddl...@googlegroups.com

Hi FrD,

Thank you for your solution, it's clever!

However I would prefer a simpler way if possible: if I use this, I'm sure that I won't remember what it means when I read it again later...

Erwan
--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at http://groups.google.com/group/tiddlywiki.

Mark S.

unread,
Oct 26, 2015, 3:04:01 PM10/26/15
to TiddlyWiki
I'm not sure if I understand 100% what you want, but your first example had a couple problems. The first was that the <$set> was missing a "Value" attribute. The 2nd was that the filter chosen was guaranteed to have a match, so the default would never get selected. The following works at least in a very simple way:

<$set name="param" value="myvalue">
   <$set name="vi" filter="[field:title<param>]" emptyValue="defaultisinourstars" value="ok">
     <$text text=<<vi>>>
   </$set>
</$set>


If there is a tiddler that matches "myvalue", then "ok" is selected. If not, the emptyValue is selected.

It helps to use the "filter" option of the advanced search when trying to figure out what kind of inputs the filter is looking for.

Mark

Erwan

unread,
Oct 26, 2015, 8:36:31 PM10/26/15
to tiddl...@googlegroups.com

Hi Mark,

thank you for your answer, it's not exactly what I was looking for (I would rather avoid using a tiddler to store the value), but it got me thinking again and I found the solution!
Basically it consists in using a macro with a default value for a parameter, and using the macrocall widget (otherwise an undefined variable is interpreted as the empty string apparently). Here it is:

\define default-if-empty(v:"default") $v$

! version 1: param has a value
<$set name=param value="my-value">
<$macrocall $name="default-if-empty" v=<<param>> />
</$set>

! version 2: param is undefined
<$macrocall $name="default-if-empty" v=<<param>> />


This is probably what Mat was referring to in his answer yesterday, somehow it hit my brain only now :-)

If anyone is interested, the problem which got me thinking about that is explained here: https://github.com/Jermolene/TiddlyWiki5/issues/2044

Thank you all!
Erwan
--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at http://groups.google.com/group/tiddlywiki.
Reply all
Reply to author
Forward
0 new messages