In the first syntax (actions=<<my-action "my passed attribute">>) the macro call is being replaced by the macro content when the $button widget is rendered. Thus, the value of the actions=... is effectively:
Then, when the $button is clicked, the action code is no longer an actual macro call and, as a result, the <__param__> syntax doesn’t have a value (since that syntax only works when contained inside a macro definition).
In contrast, in the second syntax (actions="""<<my-action "my passed attribute">>""") 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, and the <__param__> value is properly set to the param value passed into the macro.
Another way this can be made to work is to use addprefix[$param$] instead of addprefix<__param__>. This performs a direct text substitution into the macro content which then replaces the macro call in the action=... parameter of the $button widget. Then, when the $button is clicked, the passed attribute is a literal text value that has already been inserted directly into the filter syntax.
Thanks @EricShulman for that explanation.
So, what does that mean performance-wise? Especially for more complex actions, or when there are many instances of a button, there will be fewer widgets initially rendered when we use the """ syntax, right? The macro call will then only be wikified when the button is clicked.
The widgets in the actions are only parsed and rendered when the actions are invoked. However, if the actions are assigned via a macro, the macro is processed when the button widget is rendered. If you are not passing parameters to your macros, you really don’t need to concern yourself with this.
The thing to avoid is action widgets in the body of a button or other invoking widget, which are rendered each time the surrounding widget is rendered and also refreshed as part of every refresh cycle.
This is also the solution to a problem that stumped me every now and then: How to pass a variable to a macro call in the actions attribute. With the """ syntax, this
works. Without it, there is no way to get that text variable assigned. Sometimes, when applicable, I referenced <<variable>> directly in the $param attribute of the macro (or wherever I needed it), but that just doesn’t yield very pretty code and often requires scrolling to find where that variable has been set.
I’m definitely gonna put that into my bag of tricks.
Last Word Count: <$text text={{{ [[$:/temp/WordCount]get[text]addsuffix[ words found at ]else[waiting on button to be clicked]] }}}/><$text text={{{ [[$:/temp/WordCount]get[modified]format:date[0hh:0mm:0ss]] }}}/>
<$button>
<$action-setfield $tiddler="$:/temp/WordCount" text={{{ [!is[system]get[text]splitregexp[\s+]count[]] }}}/>
Update Word Cound
</$button>
Yea, it’s bad practice .. But we still have such code in the core templates. The process to get rid of it is slow, because of our backwards compatibility rules. We don’t want to break older stuff accidentally.
The button widget core code executes it in the order shown above. body actions - message - ext-actions
I’m pretty sure, that I did “mis-use” that behaviour in one of my wikis. … So “just” move every action out of a body if it has a message may break some code.