Search code examples
tiddlywikitiddlywiki5

Dynamically change caption field (or just title in tabs macro) in TiddlyWiki


I'm putting together some TiddlyWiki templates, and I've run into something that would be nice to have, but I'm not sure whether it's actually possible.

I have some tiddlers that I'm including in another tiddler using the tabs macro. Each tiddler has one of two tags associated with it. I'd like to append a snippet of text to the caption in the tab view, based on which tag is associated.

I don't have a strong preference for whether this is done by adding some kind of callback to edit the caption on save, something that somehow calculates the desired caption on the fly, altering the invocation of the tabs macro to recalculate the caption on render, or somehow causing the templates to calculate the caption field.

I haven't found anything promising going through the documentation, but maybe I just haven't figured out what's relevant to my issue. I find that happens a lot.

Like, I'm sure I can write conditionals based on whether the tags exist, but I can't see any way to interpolate text into the caption field based on any kind of computation whatsoever.

For reference, here are my current macro invocations:

<<tabs [list[]] state:$:/state/tabPeriod template:PeriodTemplate>>

<<tabs [list[$(currentTab)$]] state:$:/state/tabEvent class:"tc-vertical" template:"EventTemplate">>

<<tabs [list[$(currentTab)$]] state:$:/state/tabScene template:"SceneTemplate">>

All of these lines are from different templates, that just pull a list of tiddlers and template-transclude them into tabs using the provided template. Currently, the tabs are captioned with the tiddler caption, if defined, and fall back to the title. I'd like to alter the caption, ideally without inserting too much boilerplate into the tiddlers.


Solution

  • I figured out what I need to do differently: I defined a custom macro based on the tabs macro, added the logic, and now it works fine. I basically just changed the current contents of the caption logic to:

    <$set name="tv-wikilinks" value="no">
    <$transclude tiddler=<<currentTab>> field="caption">
    <$macrocall $name="currentTab" $type="text/plain" $output="text/plain"/>
    </$transclude>
    <$list filter='[<currentTab>tag[light]]'>
    ○
    </$list>
    <$list filter='[<currentTab>tag[dark]]'>
    ●
    </$list>
    </$set>
    

    I'm not sure if I'm using the list widget correctly, but it works.