How to set currentTiddler as default value of a parameter in a procedure

\procedure edit-field(target-field, alias, target-tiddler) 

How to set the default value of the parameter target-tiddler as currentTiddler

Old technique:
Use a $let widget at the beginning of the procedure, like this:

<$let target-tiddler={{{ [<target-tiddler>!match[]else<currentTiddler>] }}}>

If you invoke the procedure with a value for target-tiddler, that value is used. If you omit the target-tiddler parameter or pass in a blank value (i.e., target-tiddler:"") the <<currentTiddler>> will be used as a default value.

New technique:
Use a $parameters widget at the beginning of the procedure, like this:

<$parameters target-tiddler=<<currentTiddler>>>

If you invoke the procedure with a value for target-tiddler, that value is used, even if it is a blank value (i.e., target-tiddler:""). If you completely omit the target-tiddler parameter the <<currentTiddler>> will be used as a default value.

-e

2 Likes

Thanks @EricShulman . That works well. I was also trying the parameter widget

I tried like this

<$parameters 
target-tiddler={{{ [<target-tiddler>] ~[<currentTiddler>] }}} 
>

But that didn’t worked. What was wrong with that filter used to assign the variable

It’s a little unintuitive, but the filter run [<target-tiddler>] has an output even if <<target-tiddler>> is undefined — in that case, the “empty” title [[]] — so your fallback filter run ~[<currentTiddler>] is never used. To get around this, you need to explicitly remove the empty result:

  • [<target-tiddler>!match[]] ~[<currentTiddler>] (as Eric did in his $let widget example)
  • or [<target-tiddler>!is[blank]] ~[<currentTiddler>]

Incidentally, field transclusions work the same way: [{!!target-field}] ~[<currentTiddler>] will always use the field value, even if target-field is empty or nonexistent. Again, you’d need to use {!!target-field}!match[] or {!!target-field}!is[blank] before your fallback value.

2 Likes

Thanks @etardiff for explaining with examples.

I want to add another parameter alias whose default value should be the title of the target-tiddler
How to define alias parameter to achieve this ?

Edit:

<$parameters 
target-tiddler=<<currentTiddler>>
alias={{{ [<target-tiddler>get[title]] }}} 
>

The above given code does the trick

[<target-tiddler>get[title]] is equivalent to [<target-tiddler>], so you should be able to simplify that parameter definition to alias=<<target-tiddler>>.

1 Like

Why there is a difference in the the way variable target-tiddler is defined by let and parameters widget in this example given by Eric ?

The $parameters widget does the assignment only when no target-tiddler parameter was passed in to the procedure. It considers a blank parameter value ("") to still be a valid parameter.

In contrast, the $let widget is checking for “!match[]”, which will be true if no target-tiddler parameter value was passed in OR if the value that was passed in is blank.

-e

1 Like