This is a stripped-down version of a single section of Grok TiddlyWiki, optimized for fast loading and readability by search engines. Some features are missing.

For the full Grok TiddlyWiki experience, please visit the wiki version of this page.

Templates vs. Macros

10th April 2021 at 9:32am

Templates and macros can both be used to include some content in multiple places. (We haven't seen how to create global macros yet so we don't have to write the same macro in every tiddler we use it in, but we'll get there soon. If you're curious, you can jump ahead now – you know everything you need to understand that section.) For instance, in the exercise where we created a template that showed a table of the fields on a meeting, we could just as well have written a macro:

\define meetingTemplate()
| !Time|{{!!at}}|
| !Participants|{{!!participants}}|
\end

For the most part, this is personal preference and a question of what makes the most sense semantically (that is, the two methods have the same effect, but one might be easier for a person to read and understand). Here are some points that might help you to decide between using a template and a macro:

  • If you need to include the same snippet multiple times with different data in a single tiddler, a macro is likely easier since you can pass it parameters.
  • On the other hand, if you want to transclude a bunch of different tiddlers into one place using the same formatting, a template is likely easier.
    • For instance, this book stores each exercise in a tiddler and uses an ExerciseTemplate to format each exercise; where the exercises appear, it simply says, e.g., {{Ex:CreateMeetingTemplate||ExerciseTemplate}} for each exercise.
  • If you want to use text substitution, a macro is your only option.
    • But you can always include a macro in a template if you need to use text substitution.
  • Templates work best using fields of a particular tiddler as input. Macros work best using parameters written directly inside the body of the tiddler as input. However, either can get data from either source if you set a variable to the appropriate value:
    <$set name=myvar1 value="xyz">
    <$set name=myvar2 value={{!!field}}>
        {{||MyTemplate}}
        <<myMacro>>
    </$set>
    </$set>

    In MyTemplate, a reference to <<myvar1>> will get the value xyz, while in myMacro, <<myvar1>> or $(myvar1)$ will get the value xyz. Similarly with myvar2 getting the value of field on the current tiddler.

    With a macro, you can also use the $macrocall widget to pass field values as parameters.

Takeaways

Takeaways are not available in the static version of Grok TiddlyWiki. Visit the wiki version of this page to study takeaways.

↑ 4: Variables, Macros, and Transclusions