How to assign a value to field within a macro?

You’ve run up against a very common misunderstanding about filter syntax. When using a filter operator, the brackets surrounding the operand value are used to indicate how to handle that operand.

  • single square brackets are used to surround literal text values
    (e.g., narrative[someUUID])
  • single angle brackets are used to surround variable references
    (e.g., narrative<fieldvalue>)
  • single curly braces are used to surround tiddler field references
    (e.g., narrative{!!fieldname} or narrative{TiddlerName})

Note that, unlike wikitext syntax which uses doubled brackets, within filter syntax the brackets are not doubled.

Thus, in your filter, you could write:

<$list filter="[narrative<fieldvalue>!tag[narrative]sort[]]">
...
</$list>

which uses the value of the fieldvalue variable as the operand value.

Alternatively, you could also write it like this:

<$list filter="[narrative{!!$fieldname$}!tag[narrative]sort[]]">
...
</$list>

which constructs a field reference operand for the $fieldname$ of the current tiddler by adding a leading “!!” prefix to the $fieldname$ parameter

Some other notes:

Instead of using a $set widget like this:

<$set name=fieldvalue value={{{ [all[current]get[$fieldname$]] }}}>

you could use the more compact $let widget:

<$let fieldvalue={{{ [all[current]get[$fieldname$]] }}}>

Within the filter syntax you can also use the slightly more performant <currentTiddler> variable instead of the all[current] filter operator, like this:

<$let fieldvalue={{{ [<currentTiddler>get[$fieldname$]] }}}>

You could also simplify it even further by skipping the “filtered transclusion” syntax (tripled curly braces) entirely, and just use doubled-curly braces to make a direct field reference like this:

<$let fieldvalue={{!!$fieldname$}}>

(note that this syntax uses doubled-curly braces, since it is NOT a within a filter!)

I realize that all these variations on syntax can be somewhat confusing, especially if you are new to TiddlyWiki scripting… but I promise you that once you get some familiarity with them, it will become second nature and the “syntax guessing game” will be a thing of the past.

Hope this helps. Let me know how it goes…

enjoy,
-e

2 Likes

What if you wanted to set a field indicating last viewed?, do tiddlers refresh if there is no change to be properagated?

Sure such a trigger may give us “enought rope to hang ourselves” but it may also allow a class of solution that are otherwise imposible.

  • I have actualy used a message catcher to trap the navigate message and perfom actions on open tiddler and did not loose a wiki because of it.

As I wrote it is possible to do it without problems. If trigger actions only open new tiddlers which contain other triggers that’s not a problem. But if those triggers also modify the text area it can cause problems or if triggers are part of view templates. Otherwise it’s pretty robust.