Including text and field values conditional on a field having a value

125 views
Skip to first unread message

Anthony

unread,
Nov 12, 2020, 7:40:37 AM11/12/20
to TiddlyWiki
All,

I have a number of cloned tiddlers showing achievements of scientists as well as their birth and death details. I've used fields called, for example, 'birth-date', 'birth-place', 'death-date' and death-place' that are then used (via a macro) to put the value of the field in the tiddler with some preceeding text, so I might have:

Born: value of 'birth-date' field (value of 'birth-place' field)
Died: value of 'death-date' field (value of 'death-place' field)

However,  some individuals are still alive so their 'death-date' field is empty and I do not want the 'Died' line to be included at all. I figured I could use some sort of conditional formatting, maybe based uipon 'filter="[has[birth-name]]"' but I cannot figure out how to do this.

I'm sure this is really straightforward and I would really appreciate some ideas how I can do this... to begin with it would be great to have some pointers only, rather than a full-blown solution, so I can start from a point where I might be able to work it out.

Many thanks,

Anthony

Eric Shulman

unread,
Nov 12, 2020, 7:56:25 AM11/12/20
to TiddlyWiki
On Thursday, November 12, 2020 at 4:40:37 AM UTC-8, Anthony wrote:
fields called, for example, 'birth-date', 'birth-place', 'death-date' and death-place'
some individuals are still alive so their 'death-date' field is empty and I do not want the 'Died' line to be included at all.

<$list filter="[<currentTiddler>has[death-date]]">
   Died: {{!!death-date}} ({{!!death-place}})
</$list>

enjoy,
-e

Anthony

unread,
Nov 12, 2020, 9:06:14 AM11/12/20
to TiddlyWiki
Thanks Eric, I guess the '<currentTiddler>' just creates a filtered list from the current tiddler and whether text is displayed depends on this being null or not.

Now, using the same methodology, I'm trying to construct an external link and tried:

<$list filter="[<currentTiddler>has[orcid]]">
   ''ORCID'': [[{{!!orcid}}|https://orcid.org/{{!!orcid}}]]
</$list>

where 'orcid' is another field... but this doesn't work (for what it's worth, this is also in a macro). Maybe I need to create the string and then put that inside the [[...]]?

Apologies, I can't figure out how to format the code in monospace in a grey-background box.

As ever, thanks for the assistance,

Anthony

Eric Shulman

unread,
Nov 12, 2020, 11:00:57 AM11/12/20
to TiddlyWiki
On Thursday, November 12, 2020 at 6:06:14 AM UTC-8, Anthony wrote:
Thanks Eric, I guess the '<currentTiddler>' just creates a filtered list from the current tiddler and whether text is displayed depends on this being null or not.

"[<currentTiddler>has[death-date]]" means "if the current tiddler has a non-blank death-date field"

Without using <currentTiddler> at the beginning of the filter, then filter would be applied to ALL tiddlers by default,
and "has[death-date]" would produce output for every tiddler with a non-blank death-date field, not just the current one.

Now, using the same methodology, I'm trying to construct an external link and tried:

<$list filter="[<currentTiddler>has[orcid]]">
   ''ORCID'': [[{{!!orcid}}|https://orcid.org/{{!!orcid}}]]
</$list>

The problem here is that TiddlyWiki syntax doesn't "nest".  That is, you can't use one kind of syntax
such as transclusion -- {{!!orcid}} -- within another, such as a link -- [[text|url]].

You can, however, *construct* the link syntax by using a macro, like this:
\define mylink() [[$(this_orcid)$|https://orcid.org/$(this_orcid)$]]

<$list filter="[<currentTiddler>get[orcid]]" variable="this_orcid">
   
''ORCID'': <<mylink>>
</$list>

Note that the filter uses "get[orcid]" rather than "has[orcid]". This not only tests to see if the "orcid" field is non-blank, but also fetches it's value, which is set into the "this_orcid" variable. Then, the $(this_orcid)$ syntax within the macro does a string substitution, based on the current value of the "this_orcid" variable.

Note also that the $(...)$ syntax can only be used within a macro.  Keep in mind that macros only do two things:
  1. replace instances of $(...)$ with values from variables, where the variables are defined *outside* of the macro
  2. replace instances of $...$ with values from parameters, where the parameters are passed into the macro
All other processing of the macro output is performed by the calling context.  In the above example, this means the resulting link syntax -- [[foo|https:/orcid.org/foo]] -- is parsed and wikified after being "returned" from the macro.

Another point about macro syntax:  if you want to use the "parameter" method to pass the "this_orcid" value into the macro, you would write something like this:
\define mylink(id) [[$id$|https://orcid.org/$id$]]

<$list filter="[<currentTiddler>get[orcid]]" variable="this_orcid">
   
''ORCID'': <$macrocall $name="mylink" id=<<this_orcid>> />
</
$list>

Note the use of the <$macrocall> widget to invoke the "mylink" macro with the "id" parameter set to the current value of the "this_orcid" variable. This is needed because you can't nest the variable reference -- <<this_orcid>> within the shorthand <<mylink>> syntax; i.e., you CANNOT write <<mylink <<this_orcide>>>> because the closing ">>" of the variable reference would be seen as ending the enclosing macro, with the second ">>" being left over to "fall out" as displayed text.

TiddlyWiki syntax is consistent, but somewhat less flexible than other languages.  While this may be a little confusing at first, once you get used to it's peculiarities, it becomes easier.
 
Apologies, I can't figure out how to format the code in monospace in a grey-background box.

That is a feature of the old GoogleGroups editing interface that they have inexplicably left out of the new interface. To view this group using the old interface (which I prefer), you can use a URL like this:


enjoy,
-e

Anthony

unread,
Nov 14, 2020, 4:10:11 AM11/14/20
to TiddlyWiki
Thanks again, Eric. I'm using the first method as I find it little easier to follow (especially for later when I may have forgotten what's going on). I'm considering how to make it more general and not just hardwired to https://orcid.org and assume I can use both $(...)$ and $...$ in the same macro... I wonder whether this is good practice or not... so maybe it would be better to pass all variables into a macro using the second method with param1=<<value1>> param2=<<value2>> etc. in the $macrocall.

Take care,

Anthony
Reply all
Reply to author
Forward
0 new messages