If-Clause in TW?

145 views
Skip to first unread message

Rene

unread,
Oct 2, 2018, 3:37:38 AM10/2/18
to TiddlyWiki
Is there an If-Clause in TW?
I want to show an image, if a checkbox is checked in the tiddler.
How can i do that?

Thx, Rene

Jed Carty

unread,
Oct 2, 2018, 4:33:37 AM10/2/18
to TiddlyWiki
You can do conditionals in tiddlywiki in a few ways, the most straight forward is the reveal widget.

Here is a quick example of what you are asking for:

<$checkbox checked='show' unchecked='hide' field='show_image'>Show Image</$checkbox>


<$reveal type='match' state='!!show_image' text='show'>
Put your image here
</$reveal>



You could also use a list widget

TonyM

unread,
Oct 2, 2018, 5:40:53 AM10/2/18
to TiddlyWiki
Also,

Filters can be used as if tests. The nicest version is when testing the value of a field.

For example if you had a field called display-code with the value yes or no, you can do this for the current tiddler;

<$list filter="[is[current]display-code[yes]" variable=null>

Do or display what is here eg the code
By setting the variable the current tiddler remains the tiddler it is in so {{!!fieldname}} works

</$list>


There if solutions out there, see evans plugins, personaly I find more value in using filters.

The setwidget can compute filters as well and tge reveal widget can also make comparisons.


Regards
Toby

TonyM

unread,
Oct 2, 2018, 5:47:30 AM10/2/18
to TiddlyWiki
Ps

The else is a filter
[is[current]!display-code[yes]]

Good if no value set.

Or
[is[current]display-code[no]]

Regards
Tony

Mohammad

unread,
Oct 2, 2018, 7:39:18 AM10/2/18
to tiddl...@googlegroups.com
While Jed and Tony gave great examples, I add the below code as an alternative method to simulate if-clause. 



\define _maca(x) This is Macro ''A'':  $x$
\define _macb(y) This is Macro ''B'':  $y$

\define polymorph(input, cond:"a")
 
<$reveal type="match" text=$cond$ default="a")>
   
<<_maca $input$>>
 
</$reveal>
 <$reveal type="match" text=$cond$ default="b")>
    <<_macb $input$>>
 </
$reveal>
\end

Examples

<<polymorph "Hi">>
<
<polymorph input:"Hello" "a">>
<
<polymorph input:"Hola" "b">>
<
<polymorph input:"Hola" cond:"b">>

And outputs:

This is Macro A: Hi

This is Macro A: Hello

This is Macro B: Hola

This is Macro B: Hola



Anyway I saw in TW 5.18.1pre some new operators such as range, and contains  have been added, I hope developers specially Jeremy consider adding more programming capabilities among them is the IF-Clause into the core!

Cheers
Mohammad

Mohammad

unread,
Oct 2, 2018, 9:25:39 AM10/2/18
to TiddlyWiki
Very good example Tony, I used your point and wrote something like this

<$list filter="[is[current]my-field[true]]" variable=null>
Show the true part!
</$list>
<$list filter="[is[current]!my-field[true]]" variable=null>
Show the false part!
</$list>

 Of course, it possible to do it by emptyMessage, but I personal like to have something simple and clear in the core!

Cheers
Mohammad

TonyM

unread,
Oct 2, 2018, 7:29:51 PM10/2/18
to TiddlyWiki
Mohammad

As mentioned by you we can use the emptyMessage to do the else, the cleanest way I have come up with is to use macro definitions to respond.

\define action-yes()
Yes
\end
\define action-no()
No
\end

<$list filter="[go[yes]]" emptyMessage=<<action-no>> ><<action-yes>></$list>

Since the filter needs to result in one of two outcomes if one was to do a filter that could have zero or many results the filter can be extended with the limit[1]

<$list filter="[fields[]prefix[menu-]limit[1]]" emptyMessage=<<action-no>> ><<action-yes>></$list>

In the above (untested) the result will be "<<action-yes>>" if there are one or more fields in the wiki with a prefix of menu-

<$list filter="[is[current]fields[]prefix[menu-]limit[1]]" emptyMessage=<<action-no>> ><<action-yes>></$list>


In the above (untested) the result will be "<<action-yes>>" if there are one or more fields in the CurrentTiddlerwith a prefix of menu-

What the above makes me think is with a clone and minor re-write of the Listwidge we could have something like this

<$if "[go[yes]]" then=<<action-yes>> else=<<action-no>>/>  <!--THIS IS A FICTION-->


Regards
Tony

Ste Wilson

unread,
Oct 3, 2018, 2:37:27 PM10/3/18
to TiddlyWiki
I'm sure someone did a plugin for this... Can't find it though..(I have had a brief look)
I appreciate that this is less than helpfull.

@TiddlyTweeter

unread,
Oct 3, 2018, 2:59:15 PM10/3/18
to TiddlyWiki
Steve

Did you mean the macro by Thomas Elmiger "IfAisB" https://tid.li/tw5/hacks.html#ifAisB%20%E2%80%93%20Compare%20if-then-else%20Style%20in%20Wikitext

Best wishes
Josiah

Ste Wilson

unread,
Oct 3, 2018, 4:53:40 PM10/3/18
to TiddlyWiki
YES!
Reply all
Reply to author
Forward
0 new messages