Two 'simple' functions but can not work out the wikitext

52 views
Skip to first unread message

Bob Jansen

unread,
Oct 1, 2020, 7:17:21 AM10/1/20
to TiddlyWiki
I am trying to implement two simple functions but can not work out the wikitext. Tones, I have tried following your cheat sheet but nothing I try appears to work.

One over arching question. When I use a $list statement to produce a subset of available tiddlers and then go through each in turn, does TW act like a loop, for each iteration I am focussed on a single defined tiddler from the subset? If so, how is this loop tiddler addressed, by <<currenttiddler>> or does this refer to the calling tiddler?

This has bearing on the second of my functions, below.

<code>
<$macrocall 
     $name="edit-list" 
     tiddler="$:/TLS/exhibition_id"
     listview="{{{ [<value>get[exhibition_name]] }}}" 
     filter="[tag[Exhibitions]!tag[Index]]" 
/>
<$list filter="[tag[Mark]]">
     <!--append exhibition_id to exhibition_id of each marked artwork tiddler-->
     <$action-setfield 
          $field="exhibition_id" 
          $value={{{ [{!!exhibition_id}addsuffix[ ]addsuffix{$:/TLS/exhibition_id}] }}}
     />
     <!--append each artwork_id to artwork_id of the exhibition tiddler-->
     <$action-setfield 
          $tiddler= {{{ [{$:/TLS/exhibition_id}] }}}
          $field="artwork_id" 
          $value={{{ [{!!artwork_id}addsuffix[ ]addsuffix{$:/TLS/artwork_id}] }}}
     />
</$list>
</code>

The first action-setfield correctly adds the selected exhibition_id to the exhibition_id field of each artwork tiddler selected through the filter.

For the second action-setfield, I am trying to append the artwork_id from the current artwork tiddler selected through the filter to the artwork_id field of the exhibition record determined by the exhibition_id selected above. This does not work, I only see the artwork_id of the last artwork record so either the append isn't working (but it is an identical statement that worked in the first action-setfield statement) or I am not seeing each artwork tiddler in turn, only the last one (hence my question above wrt to loop processing or I can not address the tiddler whose id is stored in $:/TLS/exhibition_id whilst iterating through a list of marked artwork tiddlers.

Also, I need to amend the action-setfield statements so that if the artwork_id or exhibition_id already exist in the corresponding tiddler field, then the append does not take place. I assume that I need to use a conditional operator but can not work out the format of the statements. Eric has provided an example of this type of processing in the past but I can not find it.

bobj

Bob Jansen

unread,
Oct 1, 2020, 7:30:26 AM10/1/20
to TiddlyWiki
An update.

I have noticed that if I add a <$log statement to the wikitext to try and debug what is going on, I expect to see two entries in the dev console but I see four (there are two marked artworks for the test run). Is this the result of trying to write twice into the exhibition tiddler in the middle of a $list run? Does this force two passes of the $list loop?

bobj

Saq Imtiaz

unread,
Oct 1, 2020, 8:41:23 AM10/1/20
to TiddlyWiki

One over arching question. When I use a $list statement to produce a subset of available tiddlers and then go through each in turn, does TW act like a loop, for each iteration I am focussed on a single defined tiddler from the subset? If so, how is this loop tiddler addressed, by <<currenttiddler>> or does this refer to the calling tiddler?


Within a $list widget, it does indeed act like a loop, and currentTiddler is the default variable name for the tiddler in the current position in the loop.
You can change this by specifying a variable parameter for the list widget. Then currentTiddler variable can still be used to access the "calling tiddler".

<$list filter="[tag[Mark]]" variable="mark">


<
<mark>> is the tiddler in the current position in the loop
<
<currentTiddler>> is the "calling tiddler" / value of currentTiddler outside the list loop.


</$list>


Another approach is to save the value of currentTiddler outside the list loop in a different variable name, and then access within the list by that name.

<$vars targetTiddler=<<currentTiddler>> >
<$list filter="[tag[Mark]]">



<
<currentTiddler>>  is the tiddler in the current position in the loop
<
<targetTiddler>> "calling tiddler" / value of currentTiddler outside the list loop.


</$list>
</$vars>


To make a conditional assignment you can just wrap an action-setfield widget in a list widget that tests for the condition you want.
<$list filter=....filter with my condtion.... variable="_null">
<$action-setfield ....
</$list>


Your filter should be one that only produces an output when your conditions are satisfied and otherwise produces no results.
Note that when you are uninterested in the result of the filter and only use it for testing conditions, and do not want it to overwrite currentTiddler, you can just assign it to an unused variable like _null

Lastly, note that the debugging widget you have includes two separate widgets:
$log and $action-log

$log is renderered and run when wikitext is processed, just like any other wikitext.
$action-log behaves like an action widget, and is only executed when triggered by a button widget. 
Otherwise their parameters are identical.

So use $action-log when debugging actions performed on clicking a button or other actions trigger.
It will only log on executing the actions, whereas $log will log even on opening the tiddler with the wikitext, or when it refreshes.

Reply all
Reply to author
Forward
0 new messages