How to "if var2 is empty then assign var1 to var2"

83 views
Skip to first unread message

talha131

unread,
Jul 17, 2018, 1:05:46 PM7/17/18
to TiddlyWiki

Say I have a macro

\define example(v1, v2:"")
\end

If v2 is empty than I want to assign value of v1 to v2. What’s the TW syntax for this?

Mohammad

unread,
Jul 17, 2018, 2:04:47 PM7/17/18
to TiddlyWiki
talha,
 Tell exactly what you want to do!

If you search the forum, there are topics and solution on conditional operations!

/Mohammad

talha131

unread,
Jul 17, 2018, 2:16:36 PM7/17/18
to TiddlyWiki

My apologies for lack of clarification.

I have this macro

\define colors(color:"black", txtcolor:"", bgcolor:"")
<span style="font-size:1em;color:$color$;">&#9632;
  <span style="color:$txtcolor$;background-color:$bgcolor$;border-radius:5px; margin:1em;padding:9px 12px 10px">
  $color$</span>
</span>
\end

What I want to do is, if bgcolor is undefined then use whatever value is assigned to color variable.

I searched the forum. Unfortunately I didn’t quite understand the answers there.

One more thing, instead of using $txtcolor$, I have tried <<txtcolor>> and <<__txtcolor__>> but they didn’t work. Am I correct to assume, inside HTML tags, <<>> syntax does not work?

Mark S.

unread,
Jul 17, 2018, 4:46:20 PM7/17/18
to TiddlyWiki
Assuming that you never have a color that matches "XXX", this seems to work:

\define colors2(color txtcolor bgcolor)

<span style="font-size:1em;color:$color$;">&#9632;

 
<span style="color:<<__txtcolor__>>;background-color:$bgcolor$;border-radius:5px; margin:1em;padding:9px 12px 10px">
  $color$
</span>
</
span>
\end
\define colors(color:"black", txtcolor:"", bgcolor:"XXX")
<$vars color=<<__color__>> bgcolorinit=<<__bgcolor__>>>
<$list filter="[<color>] [<bgcolorinit>] +[!prefix[XXX]last[1]]" variable="bgcolor">
<$macrocall $name="colors2" color=<<__color__>> txtcolor=<<__txtcolor__>> bgcolor=<<bgcolor>>/>
</
$list>
</$vars>
\end
<<colors "blue" "blue" "yellow" >>

If WikiText is to be taken as a serious replacement for javascript, it really needs an equality operator.

I submitted a PR for this, but it got bogged down by a discussion about naming conventions. I'm sure there's others that have submitted similar.

I was very close to working out a solution that might have been a little cleaner, when apparently the new <<__parameter__>> syntax triggered a recursion error. Oh well, maybe someone else will have an insight.

-- Mark

TonyM

unread,
Jul 17, 2018, 8:36:52 PM7/17/18
to TiddlyWiki
Talha,

The default value set in the define macro can distract you from another way of setting a default value, using the setWidgets emptyValue or listwidgets emptyMessage

In the below example should be sufficient to explain what I mean. Note the convenience of having an input fieldname value $fieldname$ and variable <<fieldname>>

\define mymacro(fieldname)
<$set name=fieldname value="$fieldname$" emptyValue="default value">
<<fieldname>>
</$set>
\end


<<mymacro>>
<<mymacro description>>

Then if the macro is calling other macros or widgets use the $macrocall widget then you can pass <<fieldname>> into those macros

Eg; In the above macro

<$macrocall $name="othermacroorwidget" inputvalue=<<fieldname>>/>


or outside the macro in wikitext

<$macrocall $name="mymacro" fieldname="description"/>
<$macrocall $name="mymacro" fieldname=<
<othermacro>>/>


<$macrocall $name="mymacro" fieldname={{!!description}}/>

  Note: field transclusion example Not tested today

The above is not using the <<__fieldname__>> method I am yet to digest. It may reduce the need for $macrocall however this is a nice method to use especially in macros
Macros that you design for re-use, they spell it out and don't rely on the position of values you pass to the macro you are calling.

Additional advanced note:
If you use recursion where mymacro calls itself mymacro such as in toc macros, it is a fact that the fieldname $fieldname$ and variable <<fieldname>> will be reused for each invocation thus they have the same name but will not have the same value. 
So if the recursion went 5 levels deep each invocation will use an independent set of variables called fieldname for each level 

Regards
Tony

TonyM

unread,
Jul 17, 2018, 8:42:46 PM7/17/18
to TiddlyWiki
An answer more specific to you case

\define mymacro(var1 var2)
<$set name=result value="$var1$" emptyValue="$var2$">
<<result>>
</$set>
\end

But the above does not address if var2 is empty

Regards
Tony

Mohammad

unread,
Jul 17, 2018, 10:53:40 PM7/17/18
to TiddlyWiki
Tony, I think you should change var1 and var2 in your set widget based on talha question

\define mymacro(var1 var2:"")
<$set name=result value="$var2$" emptyValue="$var1$">
<li>v1: $var1$</li>
<li>v2: $var2$</li>
<li>v2(result): <<result>></li>
</$set>
\end

<<mymacro test next>>
<<mymacro test>>

TonyM

unread,
Jul 17, 2018, 11:06:07 PM7/17/18
to TiddlyWiki
Thanks for spotting that, I misread the detail.
Reply all
Reply to author
Forward
0 new messages