RFC: Tiddler Hooks

,

Tiddler Hooks may ease the path to customizing the display/viewing of tiddlers.

For beginners, editing the pieces that make up a TiddlyWiki ViewTemplate is a non-trivial task. This post offers an easier and alternative (non-replacement) mechanism for achieving the same outcome.

Whether this mechanism should be taken up by the core I will leave to others. The proposal here is merely a Request For Comment (RFC).

I do this often in my TW infrastructure


To create your first Tiddler Hook

  1. Go to tiddlywiki.com

  2. Open for editing, $:/core/ui/ViewTemplate/body
    https://tiddlywiki.com/#%24%3A%2Fcore%2Fui%2FViewTemplate%2Fbody

  3. Make the changes printed in bold type below and save the tiddler.



<$reveal tag="div" class="tc-tiddler-body" type="nomatch" stateTitle=<> text="hide" retain="yes" animate="yes">

<$list filter="[all[current]!has[plugin-type]!field:hide-body[yes]]">

{{||ViewTemplateBody-BEGIN}}

<$transclude>

<$transclude tiddler="$:/language/MissingTiddler/Hint"/>

</$transclude>

{{||ViewTemplateBody-END}}

</$list>

</$reveal>

Notice there are actually two hooks: one is placed before the tiddler body, and the other after.

  1. Create a new tiddler with the name, ViewTemplateBody-BEGIN
     – Notice the name matches your first hook.

  2. Add the following wikitext to the body of ViewTemplateBody-BEGIN:


<$set name=CO value={{{ [tag[Common Operators]then[yes]else[no]] }}}>
<$reveal type=match text=yes default=<<CO>>>
<span style="background-color:yellow">''This is a Common Operator''</span>
</$reveal></$set>
  1. Save the ViewTemplateBody-BEGIN tiddler.

To view your first Tiddler Hook in action

  1. Open the tiddler titled, tag Operator.

  1. Open another Common Operators tiddler from the tag pill and prove that all such tiddlers carry the same “This is a Common Operator” message.

Summary

We edited the core tiddler $:/core/ui/ViewTemplate/body and added two hooks. We used one of those hooks to detect when a tiddler with the tag Common Operators is shown so we can display an informative (though pretty unnecessary) message.

Afterword

You can, of course, name your hooks anything you like though it certainly helps to “self-document” by naming them in accordance with their position in the TW structure (which is not necessarily a clue to their purpose). To address that, make the hook call a macro of the same name and from within the macro, call other macros named for their purpose(s).

2 Likes

After years of trial and error I think my method is pretty close to yours in this area (assuming I’m understanding you correctly), though I just add the templates using $:/tags/ViewTemplate tags vs. hacking the core $:/core/ui/ViewTemplate/body tiddler (I prefer to not edit core tiddlers unless absolutely necessary).

So, I add a $:/custom/template-top tiddler that I tag $:/tags/ViewTemplate and then a $:/custom/template-bottom tiddler that I tag $:/tags/ViewTemplate and then position them using the tag drop-down. Within those tiddlers are lines that all look like <$list filter="[all[current]tag[Project]]"><$transclude tiddler="$:/custom/templates/project" mode="block"/></$list>. In my organizer I have templates for Projects, Tasks, Contacts, Initiatives, etc…

I will say that I’m expecting this new “Cascade” feature that I hear is coming in 5.2.1 to influence how I do this in the future. The one flaw in this process is that I can’t selectively hide stuff as easily, only add things. I hope Cascade improves this.

1 Like

Hi @CodaCoder - first of all many thanks for sharing your solution!
I really appreciate it, and I always learn from such posts!

Your solution is simple and useful! I like it! It would be much easier if users are not required to edit the core tiddler!

I would also recommend to have a look on the cascades in Tiddlywiki 5.2.1p.

It’s a POC (proof of concept) – that’s all.

I am not a fan of modifying core tiddlers, either. This is merely a demonstration of a technique I adopt in my own infrastructure but sharing that in an expedient manner would be almost impossible. The best way to prove the concept is to use tiddlywiki.com and the most visible and pervasive tiddler structure in a regular tiddlywiki is the core view template.

In my own tiddlywikis I have large-grained tiddlers called “SectionViews”. They provide hooks that I use differently in different wikis. IOW, I do not need to modify system tiddlers at all.

image

2 Likes

Thank you! as I said above these solutions are invaluable and they show tips/tricks!
I hope you can find free time and share more!

Cheers
Mohammad

1 Like

I think it is a terrific idea, but that isn’t what I need.

What I need, using $:/core/ui/ViewTemplate/body as the case study:

I need that tiddler to be componentized to the hilt.

Then I need an ability to specify an override. Something that does not replace$:/core/ui/ViewTemplate/body, but overrides it with my own creation.

Say $:/my/ui/ViewTemplate/body.

And it transcludes the $:/core/ui/ViewTemplate/body components, whichever ones I want, in whatever order I want, wrapped by whatever custom stuff I’ve created.

Something like that…

That’s what the cascade filters will provide (I believe).

1 Like

If I could figure out what cascade is supposed to be (this leaves me with no clue), I’d say yes or no.

As-is, I can’t see how that does what I’m talking about.

Hey, thank-you for that info!

That’s definitely not what I’d like, and I’m on the fence over whether I’d want to ever use that for anything. I’ll need to chew on it.

All,

The way we modify the templates and when can get complex really quickly. I have being imbedded in this somewhat including recently. Here are some notes that may help on this.

But First the original post of @CodaCoder . I to have done similar things in the past. The key to your solution seems to be, before and after the body conditional content but it seems to me the same can be done with a simple two tiddlers $:/tags/ViewTemplate list-before and list-after $:/core/ui/ViewTemplate/body, I stand to be corrected, but think this may be true.

In conversations with Jeremey some new operators are coming that will allow the writing of the core view template for much more hackability. these are;

The key possibilities in the use of the cascade filter and an updated Core user interface on the pre-release , is you can trigger the following programaticaly;

  • Alternative body templates eg $:/core/ui/ViewTemplate/body
  • Alternative “whole” View/edit Template (a view edit template, somewhat equivalent to alternative layouts).

The balance of the above improvements will allow you to do much smarter things in templates and even in an alternative $:/core/ui/ViewTemplate/body basically rewriting the original @CodeaCoder solution without overwriting core tiddlers and more extensible.

These are going to allow the view edit templates (or you own by design) to respond to more customisation without template modification ActionSetMultipleFieldsWidget, programaticaly set 0-N fields new SetMultipleVariablesWidget programaticaly set 0-N variables

I expect what will come out in the wash is additional improvements to the view/edit templating mechaisium, reducing the need for core overwrites.

Ie

a lot more hooks than we can dream of.

@Charlie_Veniot the new cascade filter and its use in the templating mechaisium is a conceptual leap, and despite being somewhat involved in the discussion, it is not strait forward to conceptualise, Mario does a good job at that Github link.

I do think it will be easier to understand when it matures and we have good use case examples. And easier to chew :slight_smile:

It’s true but I think you read past my opening statement (emphasis added)…

The post is not about hacking the ViewTemplate, as I explained to @Mohammad. I used that as a means to and end, merely so I could use tiddlywiki.com as the demonstration test bed.

Editing

Is more complex to me than a new tiddler tagged $:/tags/ViewTemplate and list-before or after (attentively the tag drop down to reorder), and will cope with wiki upgrade but hey we all have different perspectives. :sunglasses:

Sure, and I like your section idea, however I am interests in defining different tiddlers and I would use your method/algorithium in a sperate body template (especially with the new features pending) rather edit $:/core/ui/ViewTemplate/body.

Let us not get in a twist, this is all at moment of flux, and “interesting times”.