If/elseif/else logic

Hi,

I know i can use the $list widget with a filter and then / else operators to mimic conditional logic, but what if I need to do this:

if condition
  do 1
elseif condition
  do 2
else
  do 3

Thanks for any suggestions!

You may be interested in Evan Balster’s condition plugin, which adds $if, $else-if, and $else widgets.

Oh nice! What’s the right way to install it under NodeJS do you know? Can I drop it under the plugins folder maybe?

No idea, I’m afraid; I don’t use NodeJS. If all else fails, dragging and dropping it directly into a wiki ought to work, and might even be the recommended method for 3rd party plugins.

1 Like

Usually there’s a github repo i clone and symlink into the plugins folder, but in this case I can’t find the author’s repo on github: EvanBalster (EvanBalster) / Repositories · GitHub.

I can make this work i think but it will require quite a bit of manual fiddling.

But before I do that, is there a way to achieve if/elseif/else with strictly TW standard filters?

Yes, technically. You’d probably want to make use of consecutive $list widgets and/or the emptyMessage attribute (which can itself contain additional $lists, demarcated with triple quotation marks if necessary.)

Can I ask why you’re unwilling to just drag and drop? That seems to be the standing recommendation… and you may find a number of useful plugins have never been published anywhere but on a TW instance.

Another built-in option might be the cascade machanism:

https://tiddlywiki.com/#Cascade%20Filter%20Run%20Prefix%20(Examples)

It will run a series of filters until one gives a non empty result and then j just returns the first result. If you can write your conditions as filters, this should work to do elseif conditionals.

/Mike

1 Like

For the giggles:

\define do_1()
....
\end
\define do_2()
....
\end
\define do_3()
....
\end

<$let what_to_do={{{ [[if_condition]then[do_1]] [[elseif_condition]then[do_2]] [[do_3]] +[first[]]  }}}>

<$macrocall $name=<<what_to_do>>/>

</$let>

Alternatively, a little shorter:

\define do_1()
....
\end
\define do_2()
....
\end
\define do_3()
....
\end

<$macrocall $name={{{ [[if_condition]then[do_1]] [[elseif_condition]then[do_2]] [[do_3]] +[first[]]  }}}/>
1 Like

Yeah, tested and works like a charm.

Just alter the filter in the macrocall widget to see which “do” happens.

\define do_1()
Do 1 !
\end
\define do_2()
Do 2 !
\end
\define do_3()
Do 3 !
\end

<$macrocall $name={{{ [[1]match[3]then[do_1]] [[2]match[4]then[do_2]] [[do_3]] +[first[]]  }}}/>

And this could handle all sorts of things, like :

\define do_1()
Do 1 !
\end
\define do_2()
Do 2 !
\end
\define do_1and2()
<<do_1>><br><<do_2>>
\end
\define do_3()
Do 3 !
\end

<$macrocall $name={{{ [[1]match[3]then[do_1]] [[2]match[4]then[do_2]] [[4]match[4]then[do_1and2]] [[do_3]] +[first[]]  }}}/>
4 Likes

The method by @Charlie_Veniot is the most semantic wikitext way with current features and no plugin! I use it and it works great and can be maintained easily!