Parametrising tiddler creation

I want the user of my wiki to be able to enter some text and then make a tiddler with the text as title, and a specific tag by clicking a button of his choice. For instance, for quickly entering a thing-to-do or an idea or a grocery. This works if I hard-code the tags. But if I try to parametrise the procedure used, it does not work. Why not?

Here is my code with only one button:

\procedure make-item(taggy:"DEFAULT")
<%if [{$:/temp/PHB/input}minlength[1]] %>
<$action-createtiddler $basetitle={{$:/temp/PHB/input}} tiddlertype="item"  tags=<<taggy>>/>
<$action-deletetiddler $tiddler="$:/temp/PHB/input"/>
<%endif%>
\end

!!Make new item

<$edit-text tiddler="$:/temp/PHB/input" size="70" tag=input />

<$button actions=<<make-item taggy:"TODO">>>Make to-do item</$button>

The new tiddler is created with the entered text for its title, and the ‘tiddlertype’ field is created with the right value. But it gets no tags, not even the default tag ‘DEFAULT’. All goes well if I hard-code the tagging in make-item (i.e. tags="TODO")
What goes wrong? How can I mend it?

Greetings,
Sjaak

Put the actions param inside quotes:

<$button actions="<<make-item taggy:'TODO'>>">

Note the use of single quotes around the taggy:'TODO' parameter value.

-e

Thanks Eric, it works! But I have no clue why the double quotes are necessary. I did not see anything about it in the docs about procedures or buttons. Did I miss something?

The documentation for https://tiddlywiki.com/#ButtonWidget clearly says that the actions parameter is:

A string containing ActionWidgets to be triggered when the key combination is detected

The $button widget’s parameter values can be

  • strings (enclosed in quotes)
  • macro/procedure calls (enclosed in doubled angle brackets)
  • tiddler field references (enclosed in doubled curly brackets)

If the $actions parameter value is a macro – $actions=<<someprocedure ...>> (without quotes) --, then that procedure is processed as soon as the $button widget is rendered. Processing the procedure simply replaces the procedure reference with a string containing the procedure’s contents. Thus, when the $button widget is clicked the actions that are performed are no longer a procedure, so no parameter substitutions occur.

In contrast, by placing the $actions parameter within quotes, the procedure call is not processed at all until the $button is actually clicked upon, so the passed in taggy:'TODO' parameter processing is performed and the internal <<taggy>> variable is properly set and applied in the $action-createtiddler widget.

-e

1 Like

Thanks for explaining! I was thinking the problem might have to do with the space in <<make-item taggy:"TODO">>. Still…

  1. Does this mean that I could perform the text substitution myself and type the hard-coded version of the button as:
<$button actions="<%if [{$:/temp/PHB/input}minlength[1]] %>
<$action-createtiddler $basetitle={{$:/temp/PHB/input}} tiddlertype='item'  tags='TODO'/>
<$action-deletetiddler $tiddler='$:/temp/PHB/input'/>
<%endif%>" >Make to-do item</$button>
  1. What is a use case for explicitly not typing quotes if the action parameter is a unparametrised procedure? If there is none, and because the button DOES work then, I think the docs should recommend always using quotes, to avoid confusion like mine.

The use of quotes is in some ways universal, so they are not documented with each item but only once. Fortunately they follow the same standards as in most programming languages, it you stop and think about it the ways they work is essential and perhaps even the only way quoting can work. So learning TiddlyWiki quoting rules is a useful personal learning you can apply elsewhere.

Starting here https://tiddlywiki.com/#Literal%20Attribute%20Values

There are other ways to quote and use braces and also nesting them so you can have double quotes inside single quotes, or the reverse.