Passing a Variable to an Action Macro

As discussed in this thread, here is a piece of TW syntax lore that may not be very obvious.

The $macrocall documentation tells us that

Macro parameters are specified as widget attributes, thus allowing indirection via {{title!!field}}, <<macroname>> or {{{filter}}}

However, this does not work directly when the macro call is used in the action attribute of various widgets. Instead, you have to wrap it in triple double quotes, like so:

\define clickactions(text)
<$action-sendmessage $message="tm-notify" $param=<<__text__>> />
\end

<$let variable="SampleNotification">
  <$button actions="""<$macrocall $name="clickactions" text=<<variable>> />""" >
    Click Me
  </$button>
</$let>

@EricShulman explained it in the context of the $button widget like this:

[With triple double quotes, the] macrocall is defined as literal text that is only invoked (and wikified) when the $button widget is actually clicked. Thus, a true macro call is occurring.

Without the triple quotes, the macrocall is simply replaced by the text of the macro when the $button widget is rendered, so that any macro parameters have no meaning.

Have a nice day
Yaisog

1 Like

The tripple double-quotes are only needed, because the <$macrocall $name="clickactions" text=<<variable>> /> also contains a single double-quote character. So the parser needs the tripple quotes to know where the start and end of the string is

The significance of this syntax is that it is not obvious and not explained in the documentation (at least of the ButtonWidget).

The “usual” behavior when putting a macro call into (triple) quotes is that it remains a literal string and unevaluated, e.g.

<$let variable="""<$macrocall $name="now" format="YYYY-0MM-0DD" />""">
  <$text text=<<variable>> /><br>
  <$text text="""<$macrocall $name="now" format="YYYY-0MM-0DD" />""" />
</$let>

will twice yield the string <$macrocall $name="now" format="YYYY-0MM-0DD" />, but not today’s date.
When used in the actions attribute, however, that string is being evaluated (albeit only when the actions are initiated).

That was the reason why – at least for me – that little gem went undiscovered until Eric posted about it.

Have a nice day
Yaisog

2 Likes