Template or macro or other?

113 views
Skip to first unread message

amreus

unread,
Aug 26, 2020, 5:21:01 PM8/26/20
to TiddlyWiki

Working on my genealogy wiki where each person is a tiddler.  Obviously my wiki has lists of people all over the place - parents list, lists of children, siblings, etc.

In my lists I want to link to the person, but also show some other fields such as year of birth. Something similar to:

    Bill Henderson (1912 - 1974)

Now to the question.  To accomplish this, there appear to be 2 obvious ways to generate the output:

1) {{<currentTiddler>||someTemplate}}
2) <$list filter="..."><<formatting_macro <<currentTiddler>>>></$list>

Not knowing the internals of TiddlyWiki, I might assume using a macro would be more efficient since it only deals with exactly what is passed whereas the template transclusion would parse the entire tiddler each time? Is my intuition correct?  Are there better ways to do this? Is there a best way? Thanks.





TW Tones

unread,
Aug 26, 2020, 10:10:26 PM8/26/20
to TiddlyWiki
Amreus

From experience I can say don't worry about the more efficient just design for understanding. You can look at more efficient methods later if you get performance problems (unlikely)

I write all my templates and macros to operate on current tiddler and if I am listing, I typically let the currentTiddler variable change; I then have templates always contain

{{||templatename}} which acts on current tiddler.

In your examples
<<formatting_macro <<currentTiddler>>>>
is incorrect
<$macrocall $name=formatting_macro paramname=<<currentTiddler>>/>
will work
However you can use just 
<<formatting_macro>>
and inside it refer to $(currentTiddler)$ rather than pass it.

I am not sure your intuition is correct, it is inside the list that you are iterating multiple times, its when you call the macro or transclude the template that what it contains is evaluated, and only that. 

Regards
Tony

Felicia Crow

unread,
Aug 27, 2020, 2:49:23 AM8/27/20
to TiddlyWiki
Hi,

to add to Tony's answer: Personally I would say it depends more on your use case than on efficiency.

If there are multiple instances, not just one list, where for instance you want to have a person formatted as given in your example with nothing ever changing in your formatting a template would make the most sense.

Macros  in my experience make most sense when you are handling more dynamic content/formatting dependent on certain parameters. As an example say you have instances where you only want to show a persons current name and others where you also want to add their birth name if married. A macro call can handle this without having to set a field, which would be necessary for a template to reference it.

If there is only one instance where you have a list and want exactly this formatting you could also get away with having your template between the beginning and end tag of the list widget, using {{!!field_name}} to reference the fields you want to transclude as this would reference the field on the currentTiddler which is whatever tiddler the listwidget is currently rendering. Your example would then look something like this:

<$list filter="[your_filter]">
<$link /> ({!!birth-date} - {{!!death-date}})
</$list>

Hope this helps you in some way.

Regards,
Felicia

PMario

unread,
Aug 27, 2020, 1:15:02 PM8/27/20
to tiddl...@googlegroups.com
Hi amreus,

As the others wrote. ... It basically depends on your usecase _and_ a little bit your personal taste.

As you found out transclusions can be very handy. 

Eg: You have

title: anyTiddler
first-name: Mario
last-name: Pietsch
middle-name: J.

{{!!first-name}} {{last-name}}

will display [1]: Mario Pietsch. ............ BUT

Now the display format is hardcoded. ... What if: You have 100 tiddlers and want to change it to last-name first-name ... You'll need to edit all of them.

You can create a template eg: nameTemplate

title: nameTemplate

{{!!first-name}} {{{[all[current]get[middle-name]!is[blank]]}}} {{!!last-name}}

and call it like so:

title: anyTiddler
first-name: Mario
last-name: Pietsch
middle-name: J.

{{||nameTemplate}}

So you only need to change the template once! That's only 1 example. ... See docs [2]

Now let's say you want a list of the Pietsch clan :) It would be possible to construct several templates, but it will be much easier to have a global macro [3]. eg:

title: someLists
tags: $:/tags/Macro

\define showClan(clan)
<$list filter="[has:last-name<__clan__>]">
{{||nameTemplate}} ({{!!date-born}} - {{!!date-dec}}),
</$list>
\end

\define showClanList(clan)
<ul>
<$list filter="[has:last-name<__clan__>]">
<li> {{||nameTemplate}} ({{!!date-born}} - {{!!date-dec}})</li>
</$list>
</ul>
\end

It will be used with:

title: Pietsch Clan

<<showClan clan:"Pietsch">>  <-- be aware of : instead of = !!!

Just an other example.

The attachment can be saved and can be Drag&Drop imported to a test wiki. I hope this shows the usecases a little bit.

have fun!
mario

templates-and-macros.json

PMario

unread,
Aug 27, 2020, 1:18:00 PM8/27/20
to TiddlyWiki
Hi
I needed to fix some typos in the last post.
-m

amreus

unread,
Aug 29, 2020, 1:42:07 PM8/29/20
to TiddlyWiki
I wanted to say thanks to everyone and even if I do not always reply to every post, I do read everything and try to learn.  The explanations and examples are extremely helpful. So thank you again!
Reply all
Reply to author
Forward
0 new messages