conditional operations

705 views
Skip to first unread message

Mohammad

unread,
Jul 1, 2018, 10:13:52 PM7/1/18
to TiddlyWiki
Question:
 How does it possible to have a conditional operation in TW

  • Question
Assume based on the value of input to a macro parameter I want to do some operation like

IF CONDITION THEN
   TRUE-PART
ELSE
   FALSE-PART
END IF

  • My solution: Is it correct?

I did something like this

\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



  • Example
<<polymorph "Hi">>
<<polymorph input:"Hello">>
<<polymorph input:"Hola" "b">>
<<polymorph input:"Hola" cond:"b">>


  • Output

This is Macro A: Hi

This is Macro A: Hello

This is Macro B: Hola

This is Macro B: Hola




Is this correct? If I want to have something like below, what can I do

IF CONDITION THEN
   TRUE-PART
ELSE IF
   OTHER-PART
ELSE
   FALSE-PART  
END IF


Ste Wilson

unread,
Jul 2, 2018, 4:28:02 AM7/2/18
to TiddlyWiki
Someone (let's take a guess at Jed or Mat) did a plugin for that...
It's on here somewhere.
Dave might have it in his list.

@TiddlyTweeter

unread,
Jul 2, 2018, 7:01:23 AM7/2/18
to TiddlyWiki
Also Thomas Elmiger who made the "IF A is B macro" https://tid.li/tw5/hacks.html#%24%3A%2F_telmiger%2FifAisB might be worth a look.

Jed's "WizardWizard" https://ooktech.com/jed/ExampleWikis/WizardWizard/ is a very good show of procedural steps with basic branching.

TonyM

unread,
Jul 2, 2018, 7:05:52 AM7/2/18
to TiddlyWiki
Evan of formulas fame has also provided logical operators you can use but it can be achived with a set of listwidgets and filters with the else handled buy using the same filter with negation using !

Even using the emptyMessage in list and emptyVale in set widgets helps.

You can nest list widgets in any combination you want. I have half written a guide on this, tw equivalents to normal code structures, I can share the draft.

It takes a little to learn the filter equivalent of if tests but once mastered they can be more powerful because they are not only logical operators but act on sets of titles (which can be litteral values not just tiddler titles).

One difference is tw does not favor simple true false test as much because what it offers is designed to respond to tiddlers, tags and fields and is thus much richer.

Providing some simpler tests may assist users.

Regards
Tony

@TiddlyTweeter

unread,
Jul 2, 2018, 7:21:11 AM7/2/18
to TiddlyWiki
TonyM

I think that is a good observation. One where a bit more documentation could really help users. One for the list :-)

In a way, Thomas' "IF A is B macro", is quite a good starting point because it is de-limited. I do think its conveys part of the issue quite well because of that.

Josiah

TonyM

unread,
Jul 2, 2018, 7:26:41 AM7/2/18
to TiddlyWiki
Folks,

Here is a dump of my draft document, it is in no way finished, but will give you a feel for what I am trying to document. Please tell me what we should include.

I will try and show if then else while until etc.. in the long run

Regards
Tony

1. Sequence
One step following another
With sequence it commonly stops when you reach the bottom of a tiddler
  • 1.1 Left to right, Top then down the wiki text, the first item is displayed above the next etc... down the page
Note new line vs paragraphs
  • 1.2 Using list and a filter to get a sequence of items displayed or acted on
Using a list for sequence it commonly stops when the members of the list finish/are exhausted, it then moves to the next line of the current tiddler until you reach the bottom of a tiddler
This can be impacted by "Includes" see below.
2. Iteration
Repeat for each item in a set, ending when no more items are in the set or some condition is true/false
  • 2.1 Using the list widget with a filter that has 0 or more members
  • 2.2 An iteration can be ordered using sorting
  • 2.3 There are many ways to re-iterate, and you can re-iterate Macros, with transclusions and a lot more, most iterations will end with no more members in a list or some other condition.
3. Sorting
Choosing the order in which more than one item is listed
  • 3.1 Filtered lists permit the addition of the sort[fieldname] filter operator which can be used to sort the final list
Default is title or for tagged items the order in which they appear in the tags list field
!sort[fieldname] will reverse the order
  • 3.2 Used with iteration to support the order in which the items are acted on
4. Selection/Decision
using some condition to determine whether to display something or not
  • 4.1 A list field will list is membership if there are members. otherwise will not
  • 4.2 If you need to determine if there are one or more (not Zero members), or any other number of something
You can use the limit[n] filter operator so that limit[1] will display only once if there is one or more members
?Limit[0] works?
?Use ? starting line for a different class
  • 4.3 If you want to test there is no members?
The emptyMessage is available on the list widget and emptyValue on the set widget
  • 4.4 What if you want to list the non-members
Using the ! with the filter operators
Note default set all regular tiddlers so perhaps filter based on another condition first
  • 4.5 The reveal widget allows a section to be displayed according to a match including comparisons
Reveal widgets can also be nested
  • 4.6 Simple Comparisons
  • 4.7 Inclusion/exclusion and other selection tests
  • The do nothing case or filter but exclude
  • A tiddler but not those it is the prefix of
[prefix[tiddlername]suffix[tiddlername]] is both its prefix and its suffix
  • 4.8 Selection and decisions can be made based on values that are in variables, Tiddlers, fields and more
so review Using Values and variables or Setting Values and variables
5. Nesting
This is the process where one set of actions occur within another set of actions, With tiddlywiki nesting can occur for many levels "deep"
This is how Case and Recursion below, amongst other structures are possible.
  • 5.1 One implicit example of nesting is a Single ListWidget in a particular tiddler, it is in fact nesting its result in the existing tiddler
  • 5.2 In most cases anything you write can be nested within something else, and as many deep as you choose.
However if you nest your current tiddler in the current tiddler you may face infinite recursion (like between two mirrors)
See 7.x Recursion for intentional use of this ability
  • 5.3 Example Nesting list widgets

<$list filter="filter1">
Do this for each filter1 case
   <$list filter="filter2">
   Do this for each filter2 case
       <$list filter="filter3">
           Do this for each filter3 case
          <$list filter="filter4">
             Do this for each filter4 case
          </$list>
       </$list>
   </$list>
</$list>
Notes:
  • If the number of levels of nesting can vary or is large or unknown using recursion may be a better solution.
  • If transclusion or macro are used to include additional content these are in effect nested where they are included, and may contain other nesting themselves.
  • Each filter applies to the CurrentTiddler as determined by the previous ListWidget unless a variable=variablename is set.
  • Each filter starts with an assumption you are filtering from all tiddlers, so use a variable or currentTiddler value from the previous List widget for logically nested lists.
6. Case
When you want something to occur differently for each case or member in a set
6.1 A simple list allows you to do something for each member in a list which is a simple example of the "Case" structure.
In the following example each member of the set becomes the current tiddler and everything in "Do this for this case" will be applied to each tiddler.
<$list filter="yourfilter">
Do this for each case
</$list>
6.2 Treating specific cases differently
note in the below skeleton that the "specific case" tests are nested at the same level within the "yourfilter" outer listWidget
<$list filter="yourfilter">
Do this for each case
   <$list filter="specificcase1filter">
       Do this for each specificcase
   </$list>
   <$list filter="specificcase2filter">
       Do this for each specificcase2
   </$list>
</$list>
 
very sophisticated case structures can be built with the power of each list filter, emptyMessage
examples to follow
  • One statement for a subgroup or list of values
  • Multiple actions for each case
7. Recursion
A process calling itself
  • 7.1 Using ListWidgets in a tiddler or macro to call itself allows you to perform recursive loops
For example building a TableOfContents which list the tiddlers tagging the top tiddler, then all tiddlers tagging those, then all tiddlers tagging those...
The advantage of the list widget is it typically finishes after the last item in the list is processed, ensuring you eventually exit all recursion levels
See WikiPedia on Recursion
8. Includes
including content in one location that is found in another
  • 8.1 Macros
  • 8.2 Transclusion
9. Using Values and variables
Set/Vars/Params
EmptyValue/EmptyMessage
Auto seting in lists
titles of tiddlers vs pseudo titles from lists
10 Setting Values and variables
Often values and variables can be set in the context they are used as seen in Using Values and variables, however we often want the user to supply information for our Values and Variables
  • Select
  • Checkbox
  • Edit
11. Accessing last or previous values

Mohammad

unread,
Jul 2, 2018, 7:50:39 AM7/2/18
to TiddlyWiki
Thank you Josiah!
 Yes, Thomas' code is very similar to what I asked.

/Mohammad

Mohammad

unread,
Jul 2, 2018, 7:58:08 AM7/2/18
to TiddlyWiki
Hi Tony!
 This is great and helpful, worth to put somewhere to be accessible later. In this forum is hard to find and follow stuffs later.

Cheers
Mohammad

TonyM

unread,
Jul 2, 2018, 9:00:26 AM7/2/18
to TiddlyWiki
I am building a community website for that :)

It will take time because I need to prepare my tax and find commissions as an IT Consultant.

Regards
Tony

Mohammad

unread,
Jul 2, 2018, 1:50:05 PM7/2/18
to TiddlyWiki
Good luck Tony!
Hope people can make money from TW 😂
Why not!

TonyM

unread,
Jul 2, 2018, 6:08:55 PM7/2/18
to TiddlyWiki
Mohammad,

I was not talking about making money from TiddlyWiki above, Perhaps we can, but "making money with (rather than from) tiddlywiki" may be more apt.

I may however post on this soon at tiddlywiki.psat.com.au

Regards
Tony

TonyM

unread,
Jul 2, 2018, 6:18:20 PM7/2/18
to TiddlyWiki
Mohammad,

I will build this document here https://tiddlywiki.psat.com.au in the tiddler 

TiddlyWiki code structure

Published later today

Regards
Tony

On Monday, July 2, 2018 at 9:58:08 PM UTC+10, Mohammad wrote:

Andreas Hahn

unread,
Jul 2, 2018, 7:35:23 PM7/2/18
to tiddl...@googlegroups.com

Hi Tony,

I figured this might be of interest for your collection, there is another way to do conditionals within certain limits if one has the "Tinka" plugin installed. Specifically, it includes a plugin similiar to the reveal widget in its function but also allows you to check for regex expressions. An example use (taken straight from the error-checking section of Tinka):

<!-- Check whether a plugin title was given -->
	<$tinka-check text={{!!create-title-org}} pattern="^[^\s]+$">
		<$tinka-filter verb="fail">
			<$action-setfield $tiddler=<<TinkaVars>> result="true"/>
			<$action-setfield $tiddler=<<TinkaVars>> errorTitle="The plugin title is not allowed to be empty or contain whitespaces."/>
		</$tinka-filter>
                <$tinka-filter verb="pass">
                        <!-- Do something else, plugin title is valid. -->
                </$tinka-filter>
	</$tinka-check>


This is entirely undocumented, but I think its a neat way to do a conditional entirely with action widgets (all of those are). Note that "pattern" could also just be a value of another tiddler field, in which case the verbs "pass" and "fail" would correspond to "match" and "nomatch" for the reveal widget. It does only work for action widgets though, since nothing is rendered.


/Andreas

TonyM

unread,
Jul 2, 2018, 8:22:16 PM7/2/18
to TiddlyWiki
Thanks Andreas,

Captured for future publishing, and possible reference.

I would love to make my own widgets, one day.

I have started working on the use of macros which return a true or false value then have a simple to read filter that interrogates this status. I have not yet posted about it, because there is some strange intermittent behaviour I have not identified. 

The Test will look something like this

<$list filter="[enlist<hascaption>prefix[true]]" variable="null">
Has caption
</$list>

So in this case we use a "textual" description of the condition we are testing eg; hascaption

Regards
Tony

Mohammad

unread,
Jul 2, 2018, 10:43:22 PM7/2/18
to TiddlyWiki
Tony!
 I believe it should be possible to make money with TW. Why not! I believe it has this potential, while it is free and open source. It is great tool and can be used inside companies for many different application.

Everybody around me got familiar with it, downloaded and start using it in his/her daily work. I see people use it to prepare minutes of meeting. Before that they used MS Word. They dispatch their minutes of meeting as PDF.  I know some professors in use their course notes with Tiddlywiki, before that they used MsWord! Of course many people don't like to put their stuff on the internet, if so we could address here. Another case, I see students use it as database (using JSON tiddlers) to store and retrieve chemical data. In one other case I see it is used for putting flyers. 

If you focus just on the basic and standard version for storing, retrieving stuffs you will see many cases of application.

I myself not intent to make money, but I really wish others who spend a lot of time get money from/with TW.
I love TW and I want to have it alive, free and open source and have it better and better each year.

I really appreciate the great work of developers.

Thank you all
Mohammad

Mohammad

unread,
Jul 2, 2018, 10:47:43 PM7/2/18
to TiddlyWiki
That is great Tony!
Just some points:
 What do you think if use the left bar for table of contents?
 There are some stuffs prepared by others like Tobias Beer, Jed carty, Dave Graford, what do you think if you merge part of them here, those part are useful?
Is it possibe to invite others to help and have a community to prepare, edit and review the contents?


Thank you again Tony

/Mohammad

TonyM

unread,
Jul 3, 2018, 12:53:57 AM7/3/18
to TiddlyWiki
 What do you think if use the left bar for table of contents?

    Sounds like a good idea, but I have not done it yet.
 
 There are some stuffs prepared by others like Tobias Beer, Jed carty, Dave Graford, what do you think if you merge part of them here, those part are useful?

   You mean similar documentation?, at the moment I will use all I can find to improve mine, but would like to write it in my words/structure
   Any community resource would of course reference them all
 
Is it possible to invite others to help and have a community to prepare, edit and review the contents?

   Yes, I am working on that and can already with Yammer, if you are Mohammad Rahmani you are a member already visit this document to collaborate on it https://www.yammer.com/tiddlywiki/#/files/140794632

if not  visit https://www.yammer.com/tiddlywiki to request an invite
 

Thank you too Mohammad

Tony 

Mohammad

unread,
Jul 3, 2018, 1:49:57 PM7/3/18
to TiddlyWiki
Hi Tony!
 I hope I can help. Yes I am a member of Yammer!
/Mohammad

Mohammad

unread,
Aug 10, 2018, 12:46:15 PM8/10/18
to TiddlyWiki

Hello Andreas


How this code can be used as standalone macro? 
Is it possible to use without installing tinka?

This is very demanding in coding TW

Mohammad 
Reply all
Reply to author
Forward
0 new messages