Condition: An if-else plugin for wikitext

608 views
Skip to first unread message

Evan Balster

unread,
Dec 30, 2017, 12:54:54 AM12/30/17
to tiddl...@googlegroups.com
Introducing the condition plugin.  (Version 0.1 attached)

Latest version here:  https://evanbalster.com/tiddlywiki/condition.json  (Drag this plugin file into your wiki.)

It provides $if, $else and $else-if widgets that choose whether to show or hide their contents based on simple text conditions.  It pairs well with my formula plugin and its default "truthy" conditions handle boolean values.

As compared with the closely-related $reveal widget, conditions are simpler, behave more like $list, and can be executed as a chain.  They don't retain contents or animate, and can be based on variables, filtered or transcluded attributes rather than just state tiddlers.

The $else and $else-if widgets have some special uses, depending on what they're placed after...
  • After a $list widget, it will display when the list is empty.
  • After a $reveal widget, it will display when the former's contents are hidden.
  • After an $if or $else-if, it will display only when earlier conditions were all false.
This was a quick evening project, so it could have bugs.  Please test and remark here if you find any issues.  Future versions will probably live in the Formulas project.
condition.json

BurningTreeC

unread,
Dec 30, 2017, 2:02:05 AM12/30/17
to TiddlyWiki
Hello Evan,

This is great,
with your additions you're addressing lots of problems I'm facing right now

Thanks for your efforts and contributions!

I'd very much like to see this in the core, is there a chance?

kind regards,
Simon

Am Samstag, 30. Dezember 2017 06:54:54 UTC+1 schrieb Evan Balster:
Introducing the condition plugin.  (Version 0.1 attached, docs included)

Evan Balster

unread,
Dec 30, 2017, 2:08:49 AM12/30/17
to TiddlyWiki
Hey, Simon —

with your additions you're addressing lots of problems I'm facing right now.  Thanks for your efforts and contributions!
 
I'm solving similar problems.  :)  Happy to share.

I'd very much like to see this in the core, is there a chance?

That comes down to how TiddlyWiki's architects feel about it.  Previous discussions about $if widgets like the one here concluded that they would likely be redundant with $reveal.  For my part, though, I see $if playing a fairly different role:  Lightweight, value-oriented and $list-like as compared to the feature-rich, state-oriented $reveal widget...  The $else widget is something else entirely, with its unusual sibling-based behavior.

Anyway, I'll be interested to see how this flies with Jeremy and the others.  Keep an eye out for bugs.

BurningTreeC

unread,
Dec 30, 2017, 4:40:19 AM12/30/17
to TiddlyWiki
Thanks Evan,

I'll test it later on and will report back

Fingers crossed for the architects having a heart for this

TonyM

unread,
Dec 30, 2017, 5:08:55 AM12/30/17
to TiddlyWiki
Evan, Mate, as we say in Australia,

I have never being so happy finding someone with the same problems.

Detects if field exists AND has Value

The below tests all worked for me, 


Test 1

<$if value={{!!item-started}}>
item-started is set to {{!!item-started}}
</$if>
<$else>
No item-started set
</$else>

Test 2
Above in macro

Test 3

Attempt to pass field name rather than field contents to a macro
I can test with
<$set name=testdate value=(={{!!$datefield$}}=)>
<$if value=<<testdate>> >

body of macro

</$if>
</$set>
\end

I suppose I should be careful building this into a solution at this point.

But thanks so much.

Tony

coda coder

unread,
Dec 30, 2017, 9:30:58 AM12/30/17
to TiddlyWiki
Hi Evan

I'm getting an error here "nextSibling is undefined" using:

<$if value={{someTiddler!!someField}} match="yes">
  <td class="someClass">Stuff</td>
</$if>


The former code for reference:

<$reveal type=match state="someTiddler!!someField" text="yes" tag="td" class="someClass">
Stuff
</$reveal>


Coda

Evan Balster

unread,
Dec 30, 2017, 11:59:54 AM12/30/17
to TiddlyWiki
Whoopsie!  I found errors with the "match" parameter and when the widget is refreshed.  Late night work can be pretty sloppy, eh?

  • Fix an error when refreshing the condition widget.
  • Fix "match" attribute not working as expected.

Birthe C

unread,
Dec 30, 2017, 1:12:37 PM12/30/17
to TiddlyWiki
Thank you! Testing  your example the dog has stopped meowing and learned to bark.

Thank you for all your hard work,

Birthe

coda coder

unread,
Dec 30, 2017, 2:39:53 PM12/30/17
to TiddlyWiki


On Saturday, December 30, 2017 at 10:59:54 AM UTC-6, Evan Balster wrote:
Whoopsie!  I found errors with the "match" parameter and when the widget is refreshed.  Late night work can be pretty sloppy, eh?


Following in a close second place, the first hour of the day (day being a relative term ;))

Fix confirmed!

coda coder

unread,
Dec 30, 2017, 4:24:20 PM12/30/17
to TiddlyWiki
Hi Evan...


On Friday, December 29, 2017 at 11:54:54 PM UTC-6, Evan Balster wrote:

As compared with the closely-related $reveal widget, conditions are simpler, behave more like $list, and can be executed as a chain.  They don't retain contents or animate, and can be based on variables, filtered or transcluded attributes rather than just state tiddlers.


I'm no expert but, I think there's a reason that Jeremy chose to use a TextReference for use by the state attribute of the $reveal widget.  When I just tried to switch one out for the $if widget, I hit a snag:

<$reveal type="match" state="someTiddler!!$switch$" text="yes">

Note that the field part of the reference is supplied as a parameter to the outer macro wherein this line occurs.  I don't see a way to directly replace the above using the $if widget -- the following of course does not work:

<$if value={{$:/.rgt/bk/author-data!!$switch$}} match="yes">

Do you think it possible to make TextReferences work with $if?  Otherwise, the neater and much more readable $if widget would need a needless variable ($wikify?) to prepare the transclusion value for its use by $if (I think).  Right?

Hope that made sense...

Coda

Evan Balster

unread,
Dec 30, 2017, 4:32:05 PM12/30/17
to TiddlyWiki
Hey, Coda —

The substitution in your example there should work fine, so I suspect the trouble lies elsewhere.  For example, this works:

\define testSearchBar(field)
<$if value={{$:/temp/search!!$field$}} match="yes">
Search is "yes"
</$if>
\end

<<testSearchBar text>>

Remember that a macro can't define only the opening tag of a widget; it needs to define the content and closing tag as well...

coda coder

unread,
Dec 30, 2017, 4:44:14 PM12/30/17
to tiddl...@googlegroups.com
Edited for typos:

Thanks Evan... I did wonder about that, too.  So, do you think this should work? (I'm trying to keep to a minimum the alien code you need to digest :)

Original:
<$reveal type="nomatch" state=<<get-authornote-state>> text="show">

$if:
<$if not value={{<<get-note-state>>}} match="show">

\define get-note-state() $:/temp/state/$(currentTiddler)$-$(_notetype)$-$(unique)$

LOL... I think I *am* going to need another var :)

Evan Balster

unread,
Dec 30, 2017, 5:27:12 PM12/30/17
to TiddlyWiki
OK, so this is a classic case of "TiddlyWiki macro expansion is confusing as heck".  You can't put a <<macro>> (or variable) inside a {{transclusion}}, either in WikiText or in attributes.  You can expand specific variables inside macros, but it uses a dollar sign syntax.

In your case, just move the curly braces to be inside the macro (or another new macro).

<$if not value=<<get-note-state>> match="show">

\define get-note-state() {{$:/temp/state/$(currentTiddler)$-$(_notetype)$-$(unique)$}}

Evan Balster

unread,
Dec 30, 2017, 5:28:12 PM12/30/17
to TiddlyWiki
Oh, wait, sorry, shoot.  That won't work at all, it'll just expand to the string of the transclusion.

Yeah, you're going to have to set another variable, or use <$tiddler> or something.

coda coder

unread,
Dec 30, 2017, 5:46:54 PM12/30/17
to TiddlyWiki


On Saturday, December 30, 2017 at 4:28:12 PM UTC-6, Evan Balster wrote:
Oh, wait, sorry, shoot.  That won't work at all, it'll just expand to the string of the transclusion.

Right.  And why I said (wished) $if accepting a TextReference (like state does for $reveal).  I'm hinting that if a preparse returned you something with !! then you could support them (and then the wikification would work - right?)

But it's probably a step too far (plus you'd have the only widget that supported both on the entire planet)  ;)  Too confusing is my guess.

Evan Balster

unread,
Dec 30, 2017, 5:48:47 PM12/30/17
to TiddlyWiki
Honestly, stuff like that is why I'm making the formula widget.  TiddlyWiki doesn't do nested expressions, formula does.  The only missing piece is a transclude function accepting a string (or a family of these...).

Evan Balster

unread,
Dec 30, 2017, 5:52:58 PM12/30/17
to TiddlyWiki
Qualifying the last message because I spoke a bit too quickly and don't want to be blithe:

TiddlyWiki doesn't do nested string expressions, macros notwithstanding, and Jeremy has suggested that the current design forces too much dependence on macros.  That's a motivator for my work on the formula plugin.

Diego Mesa

unread,
Feb 2, 2018, 4:00:54 PM2/2/18
to TiddlyWiki
Hey Evan,

<$if value="(=gt(2,3)=)">
yes
</$if>
<$else>
no
</$else

I can't get no to show, even with

  • value=(=gt(2,3)=)
  • value="gt(2,3)"
  • value="(=gt(2,3)=)"

The only way I can is by setting:


value="FALSE"


myself. Even though (=gt(2,3)=) does indeed return FALSE.

Reply all
Reply to author
Forward
0 new messages