[TW5] Simple comparison

131 views
Skip to first unread message

TonyM

unread,
Nov 6, 2017, 7:15:46 AM11/6/17
to TiddlyWiki
Folks,

I have no idea where to start on this although I can make use of it in a few places.

If I have a Global macro that sets;
\define whikiname() MyWikiName

Such that I can use  <<whikiname>> any where to return the value "MyWikiName"

Is there a way I can do something if the Current Tiddler has the same name, or the same name with a known suffix

eg Pseudo-code

If ((!!title)) equals <<whikiname>> do
This
End

If ((!!title)) Not Equals <<whikiname>>
 do This
End

and Similarly detect test against other firlds or

if <<whikiname>> empty

The Same to test a field value against State Tiddler also may help

Thanks
in advance
Tony

Jed Carty

unread,
Nov 6, 2017, 7:44:54 AM11/6/17
to TiddlyWiki
You can use the list widget you can do this:

<$list filter='[<whikiname>prefix<currentTiddler>suffix<currentTiddler>]'
emptyMessage="They Don't Match">
They match
</$list>

TonyM

unread,
Nov 6, 2017, 6:07:10 PM11/6/17
to TiddlyWiki
Thanks Jed,

That is Quite elegant,

So as I understand it, it lists titles where the <<wikiname>> has currentTiddler both as its prefix, and as its suffix which can only be true if they are the same.

Thanks Heaps
Tony

Eric Shulman

unread,
Nov 6, 2017, 6:46:38 PM11/6/17
to TiddlyWiki
On Monday, November 6, 2017 at 4:15:46 AM UTC-8, TonyM wrote:
If I have a Global macro that sets;
\define whikiname() MyWikiName
Such that I can use  <<whikiname>> any where to return the value "MyWikiName"
Is there a way I can do something if the Current Tiddler has the same name, or the same name with a known suffix

If ((!!title)) equals <<whikiname>> do
This
End

If ((!!title)) Not Equals <<whikiname>>
 do This
End

You can use the $reveal widget to render conditional content.

Normally, $reveal takes a "state", "type " and "text" params, and compares a value *stored in the text field of a tiddler* (the state) against the specified text value (which can be literal text, a variable, or a tiddler field reference).  The type param is either "match" or "nomatch" (i.e., "equals" or "not equals").  Thus, assuming that "SomeTiddler" contains the state value, you can write conditional code like this:

<$reveal state="SomeTiddler" type="match" text={{!!title}}>
   current tiddler's title matches content in SomeTiddler
</$reveal>
<$reveal state="SomeTiddler" type="nomatch" text={{!!title}}>
   current tiddler's title does NOT match content in SomeTiddler
</$reveal>

Of course, it is not always convenient (or even possible) to use a state tiddler all the time.  Sometimes -- as in your requested use-case -- the value to compare is stored in a variable (i.e., <<whikiname>>).  Fortunately, there is a variant usage of $reveal, that can compare ANY two values, by using the "default" param, which is applied when no "state" param is supplied.  Thus:

<$reveal text={{!!title}} type="match" default=<<whikiname>>>
   title matches the value of <
<whikiname>>
</$reveal>
<$reveal text={{!!title}} type="nomatch" default=<
<whikiname>>>
   title does NOT match the value of <
<whikiname>>
</$reveal>

Note that the value for the "default" param can be any valid reference, not just a variable.  Thus, it is possible to compare ANY two fields, from any tiddler you want, like this:

<$reveal text={{!!title}} type="match" default={{!!otherfield}}>
   title matches the value stored in "otherfield"
</$reveal>
<$reveal text={{!!title}} type="nomatch" default={{!!otherfield}}>
   title does NOT match the value stored in "otherfield"
</$reveal>

You might also note that the default value could be a reference like {{SomeTiddler!!text}}, which effectively reproduces the standard $reveal usage.  In other words, the following two code excerpts are equivalent:

<$reveal state="SomeTiddler" type="match" text={{!!title}}>...
   and
<$reveal text={{!!title}} type="match" default={{SomeTiddler!!text}}

The default value can also be literal text, which allows you to check a variable for an empty value, like this:

<$vars somevariable="foo">
<$reveal text=<
<somevariable>> type="match" default="">
  blank
</$reveal>
<$reveal text=<
<somevariable>> type="nomatch" default="">
  not blank
</$reveal>
</$vars>


Hopefully, the above is enough to address your particular use-case needs or, at least, enough to point you in the right direction.

enjoy,
-e
Eric Shulman
TiddlyTools.com: "Small Tools for Big Ideas" (tm)
InsideTiddlyWiki: The Missing Manuals
  

TonyM

unread,
Nov 6, 2017, 7:14:15 PM11/6/17
to TiddlyWiki
Thanks Eric for being a "Brains Trust",

I see that the <$reveal> is good for comparisons without having to juggle filters, and answers my need to include more in response to no match.

I am currently Generalising Jeds solution for the different variable and reference types, and expect to do so with your suggestion. I plan to post this back here for the future enquiry.

Thanks
Tony
Reply all
Reply to author
Forward
0 new messages