The first line of the definition specifies the macro name and any parameters. Each parameter has a name, and optionally a default value that is used if no value is supplied on a particular call to the macro. The lines that follow contain the macro's text (i.e. the snippet represented by the macro name), until
\end appears on a line by itself:
\define sayhi(name:"Bugs Bunny" address:"Rabbit Hole Hill") Hi, I'm $name$ and I live in $address$. \end
Alternatively, the entire definition can be presented on a single line, without an
\define sayhi(name:"Bugs Bunny") Hi, I'm $name$.
A more formal presentation of this syntax is also available.
The snippet can contain placeholders for parameters. These consist of a parameter name between dollar signs, like
It can also contain placeholders for variables. These consist of a variable name (or macro name) between dollar signs and round brackets, like
The actual value of the parameter or variable is substituted for the placeholder whenever the macro is called:
\define say-hi-using-variables() Hi, I'm $(name)$ and I live in $(address)$. \end \define name() Bugs
<$set name="address" value="Rabbit Hole Hill"> <<say-hi-using-variables>> </$set>
The parameters to a wikitext macro are also available as special variables named as the parameter name wrapped in double underscores. For example, the example above could also be expressed as:
\define sayhi(name:"Bugs Bunny") Hi, I'm <$text text=<<__name__>>/>.
Accessing parameters as variables only works in macros that are wikified and not, for example, when a macro is used as an attribute value. The advantage of the technique is that it avoids the parameter value being substituted into the macro as a literal string, which in turn can help avoid issues with parameters that contain quotes.
For example, consider this macro. It is intended to wrap a DIV around another macro invocation, passing through the single parameter to the inner macro:
\define related-tags(base-tag) <div class="wrapper"> <$macrocall $name="anothermacro" param="""$base-tag$"""/> </div> \end
The code above will fail if the macro is invoked with the argument containing triple double quotes (for example
<<related-tags 'Triple """ Quotes'>>). Using parameter variables offers a workaround:
\define related-tags(base-tag) <div class="wrapper"> <$macrocall $name="anothermacro" param=<<__base-tag__>>/> </div> \end
Macros are available to the tiddler that defines them, plus any tiddlers that it transcludes.
To make a macro available to all tiddlers, define it in a tiddler that has the tag