This page is part of a static HTML representation of the TiddlyWiki at https://tiddlywiki.com/

Variable Usage

 26th July 2023 at 2:59pm

Ways to define variables and parameters

how declared how parameters are defined accessing parameter values in the body
\define()$param$, <<__param__>>
$parameters or \parameters<<param>>
$set, $let, $vars$parameters or \parameters<<param>>
\procedure, \widget(), $parameters or \parameters<<param>>
\function()<param>
javascript macrosexports.params javascript property arraypassed as normal javascript function parameter and so accessed as a normal javascript variable

Examples

These examples are meant to provide insight into the various ways of defining and using parameters. In many cases they do not illustrate best practices.

\define

\define mp1(a1)  $a1$ - <<__a1__>>

\define mp2() <$parameters a1><<a1>></$parameters>

\define mp3()
\parameters(a1)
<<a1>>
\end

|<<mp1 foo>>|<<mp2 foo>>|<<mp3 foo>>|

$set, $let, $vars

<$set name="sp1" value="<$parameters a1><<a1>></$parameters>">
<$set name="sp2" value="""
\parameters(a1)
<$parameters a1><<a1>></$parameters>
""">
<$vars vp1="<$parameters a1><<a1>></$parameters>" vp2="""
\parameters(a1)
<$parameters a1><<a1>></$parameters>
""">
<$let lp1="<$parameters a1><<a1>></$parameters>" lp2="""
\parameters(a1)
<$parameters a1><<a1>></$parameters>
""">

|<<sp1 foo>>|<<sp2 foo>>|
|<<vp1 foo>>|<<vp2 foo>>|
|<<lp1 foo>>|<<lp2 foo>>|
</$let>
</$vars>
</$set>
</$set>

\procedure, \widget

\procedure pp1(a1)  <<a1>>

\procedure pp2() <$parameters a1><<a1>></$parameters>

\procedure pp3()
\parameters(a1)
<<a1>>
\end

\procedure wp1(a1)  <<a1>>

\widget wp2() <$parameters a1><<a1>></$parameters>

\widget wp3()
\parameters(a1)
<<a1>>
\end

|<<pp1 foo>>|<<pp2 foo>>|<<pp3 foo>>|
|<<wp1 foo>>|<<wp2 foo>>|<<wp3 foo>>|

\function

\function fp1(a1)  [<a1>]
|<<fp1 foo>>|

Behaviour of invoked variables depends on how the variable was declared

Invoked in normal wikitext context: <$transclude $variable=macro/> or <<macro>>

how declaredbehaviour
\defineAll wikitext and variable substitution and textual substitution takes place
$set, $let, $vars, \procedure, \widgetAll wikitext and variable substitution takes place
\functionInvoking a function in this way (<<macro>>) is a synonym for <$text text={{{[function[macro]]}}}/>. As with any filtered transclusion (i.e. triple curly braces), all results except the first are discarded

Invoked via widget attribute: <div class=<<macro>>/>

how declaredbehaviour
\defineTextual substitution of parameters is performed on the body text. No further processing takes place. The result after textual substitution is used as the attribute's value
$set, $let, $vars, \procedure, \widgetBody text is retrieved as-is and used as the attribute's value.
\functionWhen a function is invoked as <div class=<<macro>>/>, it is a synonym for <div class={{{[function[macro]]}}}/>. As with any filtered transclusion (i.e. triple curly braces), all results except the first are discarded. That first result is used as the attribute's value. Note that functions are recursively processed even when invoked in this form. In other words a filter expression in a function can invoke another function and the processing will continue

Invoked via filter operator parameter: [<macro>]

how declaredbehaviour
\defineTextual substitution of parameters is performed on the body text. No further processing takes place. The result after textual substitution is used as the filter operator's parameter.
$set, $let, $vars, \procedure, \widgetBody text is retrieved as-is and used as the filter operator's parameter.
\functionThe body text of the function is treated as a filter expression and evaluated. The first result is passed to the operator as a parameter. The remaining results are discarded.

Invoked via function call in a filter expression: [function[macro]]

how declaredbehaviour
\define, $set, $let, $vars, \procedure, \widgetEvery function is a variable, but only variables defined using \function are invokable using the function filter operator. Attempts to use a non-function variable is the same as if the function doesn't exist. The behavior in this case is like the identity function. All filter input is passed unchanged to the output.
\functionThe body text of the function is treated as a filter expression and evaluated. This filter expression can itself contain a function call. Filter expressions can be factored out into functions arbitrarily deep.

Examples

Below is an example macro, procedure and function definition. All three forms of parameter substitution $a1$, <<__a1__>>, and <<a1>> are included in each definition. The output helps illustrate when each form of substitution will or will not have affect.

\define m1(a1) $a1$ - <<__a1__>> - <<a1>>
\procedure p1(a1) $a1$ - <<__a1__>> - <<a1>>
\function f1(a1) $a1$ "-" [<__a1__>] ="-" [<a1>] :and[join[ ]]
Variable transclusionoutput
<<m1 foo>>foo - foo -
<<p1 foo>>$a1$ - - foo
<<f1 foo>>$a1$ - - foo
Widget attributeoutput
<$text text=<<m1 foo>>/>foo - <<__a1__>> - <<a1>>
<$text text=<<p1 foo>>/>$a1$ - <<__a1__>> - <<a1>>
<$text text=<<f1 foo>>/>$a1$ - - foo
Filter operator parameteroutput
[<m1 foo>]foo - <<__a1__>> - <<a1>>
[<p1 foo>]$a1$ - <<__a1__>> - <<a1>>
[<f1 foo>]$a1$ - - foo
Function call in filter expressionoutput
[function[m1],[foo]]"A free, open source wiki revisited" by Mark Gibbs, NetworkWorld
[function[p1],[foo]]"A free, open source wiki revisited" by Mark Gibbs, NetworkWorld
[function[f1],[foo]]$a1$ - - foo

Namespaces

  • tiddler titles - tiddlers are uniquely identified by their title. The namespace for tiddler titles and variable names are completely separate.
  • variables - \define, $set, $let, $vars, \procedure, \widget, \function all create variables. If the same name is used, then later define will overwrite earlier defined
  • function filter operator parameter - only variables defined using \function can be called using the function operator
  • filter operators - only the javascript defined filter operators and variables defined using \function with name containing a dot can be called
  • widgets - variables defined using \widget can be invoked using <$widget/> syntax ONLY if the name starts a dollar sign. Without the dollar sign prefix, defining variables using \widget is no different than using \procedure.