Is it possible to automatically generate multiple templates or macros?

143 views
Skip to first unread message

Si

unread,
Jun 15, 2021, 11:28:36 AM6/15/21
to TiddlyWiki
I've been experimenting with using "typed" links in my wiki, inspired by this post from a few months back.

For example if I want to link to a food item, but also indicate that it is an ingredient, I write:

{{tomatoes||ingredient}}

Where I have the following "type" tiddler:

title: ingredient
tags: type
text: <$link to=<<currentTiddler>><<currentTiddler>></$link>


I'm wondering if there is anyway to generalise this so that I don't actually have to add the template to the text field of every "type" tiddler I create? Specifically I want to leave the text field free for other content, and also be able to use any tiddler with the tag "type" in the same way. So all I would have to do is add a tiddler with the title "ingredient" and the tag "type" and immediately be able to use it as a link template.

Alternatively maybe there is a way to generate multiple macros in a similar way? A tiddler with the title "ingredient" and the tag "type" would trigger the implicit creation of an "ingredient" macro.

I can't see how this would be possible, but I'm wondering if there's a trick I'm missing?

Si

unread,
Jun 20, 2021, 6:32:59 AM6/20/21
to TiddlyWiki
I thought I'd give this a bump just in case there's anyone out there who missed it that can help me...

TW Tones

unread,
Jun 20, 2021, 8:11:23 PM6/20/21
to TiddlyWiki
Si,

I see what you want to achieve, I think the functionality is possible (perhaps not literally though), and I will give it more thought, but I would expect it is a little problematic because you are making use of the short form of the transclude statement, and you want that to do extra work, such as making use of a template to the transcluded "template", a bit like a view Template for transclusions.

My initial thought is to use a macro instead, even if that macro then invokes a "transclusion view template" and then the transcludes the named template.

<<ingredient tomatoes>>

But if you want to generalise even further
<<object ingredient tomatoes>> which could detect if "ingredients" is defined, then if "tomatoes" is defined and provide create buttons, but once created seek the ingredients and ingredient details and display as you macro deems.

The thing is macros allow for higher levels of abstraction, the inclusion of code detecting "existence" and offering to create custom tiddlers, not to mention determining the formatting of the display of content. I also add to macros mode or state switches, for example if the wiki has $:/config/author-mode = yes then provide the new buttons if the object "ingredients" or instance "tomato" do not exist, otherwise indicate a missing object/ingredient.

An approach is to use a macro with a list widget where ingredients is the name of the template the list widget uses.

Finally remember tiddlers (or fields for that matter) that are transcluded can also transclude multiple levels deep.

Regards
Tones
Message has been deleted

Si

unread,
Jun 22, 2021, 3:12:57 PM6/22/21
to TiddlyWiki
Hi Tones, thanks for your comment.

>>> but I would expect it is a little problematic because you are making use of the short form of the transclude statement

I'm assuming that by "short form" you mean using curly braces rather than the transclude widget? If so how could I use the transclude widget to generate multiple templates?

>>> My initial thought is to use a macro instead, even if that macro then invokes a "transclusion view template" and then the transcludes the named template.
>>>
>>> <<ingredient tomatoes>>

Yes if this is possible then great, but how could I actually do it? I would want to be able to generate a new macro for every tiddler with the tag "type". As far as I can tell you can't define macros within a list widget.

>>> <<object ingredient tomatoes>> which could detect if "ingredients" is defined, then if "tomatoes" is defined and provide create buttons, but once created seek the ingredients and ingredient details and display as you macro deems.

Thanks I did not think of this (having a single macro which takes both "type" and "linked tiddler" as parameters). It's not ideal but it's an option I could use as a last resort. I'm not sure what you mean by "create buttons" though.

>>> An approach is to use a macro with a list widget where ingredients is the name of the template the list widget uses.

OK now I'm lost. Can you give an example of what you mean?

>>> Finally remember tiddlers (or fields for that matter) that are transcluded can also transclude multiple levels deep.

Yes but I can't see what the relevance of this is. Could you add a bit more detail?

TW Tones

unread,
Jun 22, 2021, 7:58:56 PM6/22/21
to TiddlyWiki
Si,

Comments inline


>>> but I would expect it is a little problematic because you are making use of the short form of the transclude statement

I'm assuming that by "short form" you mean using curly braces rather than the transclude widget? If so how could I use the transclude widget to generate multiple templates?

Yes, rather than curly braces you can use the tiddler widget to set a current tiddler and the transclude widget to name the tiddler. This allows you to add additional logic if an when needed.
 

>>> My initial thought is to use a macro instead, even if that macro then invokes a "transclusion view template" and then the transcludes the named template.
>>>
>>> <<ingredient tomatoes>>

Yes if this is possible then great, but how could I actually do it? I would want to be able to generate a new macro for every tiddler with the tag "type". As far as I can tell you can't define macros within a list widget.

I will give this some thought, but no need for a new macro each time, just a macro that responds to parameters it is given. Personally I prefer to manage different tiddler types with a fieldname containing only one value eg; object-type=task rather than tags.
 

>>> <<object ingredient tomatoes>> which could detect if "ingredients" is defined, then if "tomatoes" is defined and provide create buttons, but once created seek the ingredients and ingredient details and display as you macro deems.

Thanks I did not think of this (having a single macro which takes both "type" and "linked tiddler" as parameters). It's not ideal but it's an option I could use as a last resort. I'm not sure what you mean by "create buttons" though.

Well, lets say I use <<object ingredient new-food>> a button would appear to create new-food
 similarly

<<object task taskname>> a button to create the task object and taskname instance would appear.

However if both exist just display or transclude.



>>> An approach is to use a macro with a list widget where ingredients is the name of the template the list widget uses.

OK now I'm lost. Can you give an example of what you mean?

\define show-ingredient(ingredient)
<$list filter="[[$ingredient$]] template=ingredient>
This is ignored because the tiddler "ingredient" is used to format the display of ingredient(s) and the current tiddler would be tomato
</$list>
\end
<<show-ingredient tomato>>
 

>>> Finally remember tiddlers (or fields for that matter) that are transcluded can also transclude multiple levels deep.

Yes but I can't see what the relevance of this is. Could you add a bit more detail?

Only that you could transclude in a transclusion in a transclusion ... if that helped your design. eg you can transclude a field which contains a transclusion. For example see the caption field in core buttons such as $:/core/ui/Buttons/edit

Given your questions I may do a some experiment's.

Tones

Reply all
Reply to author
Forward
0 new messages