Best practice: Templates and Procedures

I’m building a method to link two tiddlers by creating a third one, in this case for a gift ideas notebook:

title: John
tags: person
title: Bicycle
tags: gift
title: Umbrella 
tags: gift
title: Link1
tags: link todo
link-person: John
link-gift: Bicycle
title: Link2
tags: link done
link-person: John
link-gift: Umbrella

According with the above, I’m planning to get John a bicycle (todo) and I’ve already given him an umbrella (done).

On each person I’ll list all available gifts in a dropdown and I’ll display two buttons, To Gift and Already Gifted. Each will run a procedure (create-link-person-gift()) to create the appropriate new link tiddler similar to the above and clear the currently selected entry from the dropdown.

I’ve been thinking of two possible approaches:

  1. Define the whole procedure in a $:/tags/ViewTemplate, insert it into every person and access {{!!title}} and {{!!selected-gift}} relatively easily.
  2. Define the procedure in a separate tiddler tagged $:/tags/Global and call it from each person by passing as a parameter <<currentTiddler>> to set the correct context (inspired by @EricShulman’s comment) before accessing the two required fields.

I’m still struggling a bit with the appropriate syntax for passing parameters, but which of the above two do you think would be preferable? I’m leaning towards the former as it seems simpler, but I’m a bit worried it might create a lot of overhead if a lot of persons are open? For example, what would happen if I feed the above into TW5-Graph and a lot of them are displayed at the same time?

I’ve long been an advocate for that style for any data-heavy wiki.

I’m definitely in favor of the first approach. Templates make this much easier to work with. Moreover, I don’t think this is a concern:

Showing such a ViewTemplate does not happen automatically. If you transclude a tiddler in something like TW5-Graph, it most likely doesn’t get all the templates you associate with that tiddler, only some default one based around the text field. You can override by passing a specific template to the transclusion, but presumably you wouldn’t want that. TW-Graph uses little more than the title (or caption?) in the graph. (This is simply an educated guess regarding TW-Graph, but I would be quite surprised to be proven wrong.)

Please ask if you have more specific questions, but note that a ViewTemplate does not generally need parameters. It will derive what it needs from the currentTiddler variable active when it’s renedered.

1 Like

Thank you for your insight, it’s time for me to take up your offer because I’ve been banging my head since yesterday and I can’t make it work :sweat_smile:

I’ve ended up with the following ViewTemplate applied on each person:

\procedure create-link-person-gift(theTags)
	<$action-createtiddler 
		$basetitle="GiftLink" 
		link-gift={{!!temp-gift}}
		link-person={{!!title}}
		tags="link">

		<$action-listops 
			$tiddler=<<createTiddler-title>> 
			$field="tags"
			$subfilter="[<theTags>]"/>

	</$action-createtiddler>

	<$action-deletefield 
		$field="temp-gift"/>

\end create-link-person-gift

<$button
actions=<<create-link-person-gift "todo">> >
To Gift
</$button>

<$button
actions=<<create-link-person-gift "done">> >
Already Gifted
</$button>

I’ve tried all sorts of different incantations to append the todo/done tags passed as parameters and I can’t make it work. Best result I’ve had is ending up only with the link tag. I thought (and then wished) I could just add the parameter in the $action-createtiddler tags parameter but I can’t. Someone suggested using an extra $action-listops step inside but I also can’t make it work.

I think I got it! I tried @pmario’s suggestion and it worked:

\procedure create-link-person-gift()
	<$action-createtiddler 
		$basetitle="GiftLink" 
		link-gift={{!!temp-gift}}
		link-person={{!!title}}
		tags=`link [[$(theTags)$]]`>

	</$action-createtiddler>

	<$action-deletefield 
		$field="temp-gift"/>
\end create-link-person-gift

<$set name="theTags" value="todo">
	<$button
	actions=<<create-link-person-gift>> >
		To Gift
	</$button>
</$set>

<$set name="theTags" value="done">
	<$button
	actions=<<create-link-person-gift>> >
		Already Gifted
	</$button>
</$set>
1 Like