This version is out of date, covering development as far as v4.2.5. It is maintained here only for inbound reference links from elsewhere. Jump to the current version of aTbRef. |
A Tinderbox Reference File : Objects & Concepts : Macros |
Macros |
Macros are encapsulated sections of code that may be used to insert larger or more complex sections of code back into the note. They are used in conjunction with the ^do() export code and allow the user to insert the macro's output directly into exported text or HTML. They may also used inside Tinderbox to populate attributes values via actions and rules.
From v 3.0.0, macros can be copied from one Tinderbox file to another by dragging a macro from the source file's Attributes Macros pane onto a target file's View window(s) or Macros pane.
In v4.0.0, macro utility is extended from export into both in both queries and actions.
In macros, the character $ has a special meaning which will be familiar to those who've used regular expressions before:
$1 means "the first argument to the macro"
$attributeName means "the value of attributeName" (of the referenced note).
These are applied recursively, so that if we have a macro "first" whose value is: <h2>$1</h2>
Then calling the macro...
^do(first,"This parrot is $2","dead!")^
... will give us ...
This parrot is dead!
... whilst ...
^do(first,"This parrot is $2","just sleeping.")^
... will give us ...
This parrot is just sleeping.
In the above example note the need to allow for white space at string joins, so either a space between 'is' and '$2' or at the beginning of the second string.
For example, a macro named 'dot' whose value is:
<img src="anImage.gif" height="16" width="16" alt="">
... is called as ...
^do(dot)^
... and results in ...
<img src="anImage.gif" height="16" width="16" alt="">
To make this a more useful macro and allow it to set the image name, height, width, and also set the alt to the image name then change the macro's value to:
<img src="$1" height="$2" width="$3" alt="$1">
... called as ...
^do(dot,pic.gif,20,30)^
... that results in ...
<img src="pic.gif" height="20" width="30" alt="pic.gif">
If you want to remove the special meaning from $ in a macro argument, precede it with a backslash \$. Commas in text strings should also be escaped lest they be parsed as argument delimiters; similarly, \ must become \\. To insert a string like "Prices, $6 \ $16.", change the above macro to:
<img src="$1" height="$2" width="$3" alt="$4">
... and run as ...
^do(dot,pic.gif,20,30,Prices\, \$6 \\ \$16.)^
... to get ...
<img src="pic.gif" height="20" width="30" alt="Prices, $6 \ $16.">
Up: Objects & Concepts | |
Previous: Rules | Next: Adornments |
Licensed under Creative Commons Attribution-Noncommercial-Share Alike 3.0 License
[See aTbRef CC licence Attribution/Waiver info info]