Equivalent of "thisTiddler" for the previous transclusion

I am wondering if there is a good way to access the title of the tiddler which invoked the current transclusion, but that isn’t necessarily the currentTiddler.

Background: I use a decent number of custom toolbar buttons, so I currently have a procedure which I import into my new button tiddlers which lets me avoid duplicating the same code across every single one.

\whitespace trim
\procedure toolbar-button(button-tiddler,button-actions)
<$button actions=<<button-actions>> tooltip={{{ [<button-tiddler>get[description]] }}} aria-label={{{ [<button-tiddler>get[caption]] }}} class=<<tv-config-toolbar-class>>>

<% if [<tv-config-toolbar-icons>match[yes]] %>
<$transclude $tiddler={{{ [<button-tiddler>get[icon]] }}}/>
<% endif %>

<% if [<tv-config-toolbar-text>match[yes]] %>
<span class="tc-btn-text"><$text text={{{ [<button-tiddler>get[caption-text]] }}}/></span>
<% endif %>

</$button>
\end

My toolbar button tiddlers then simply look like this:

\import [the above tiddler defining the procedure]
\procedure actions() [actions go here]

<$transclude $variable="toolbar-button" button-tiddler=<<thisTiddler>> button-actions=<<actions>>/>

This is the reasonably neat and tidy, but the perfectionist in me can’t help but wonder: Is there a way to access the name of the tiddler which transcludes the toolbar button procedure from within the procedure itself, e.g. as a default value for the button-tiddler parameter, rather than having to pass it as a parameter using thisTiddler in every button tiddler?

To be clear, my current method works well enough, but I am wondering if anyone with a deeper understanding can answer my question, just for my own education.

Thanks for reading!

The new $parameters widget may contain an answer in it’s $depth parameter but I think what you have done is reasonable because you are designing it into the code.

  • I tend to let the currentTiddler vary in each use of the list widget.
    • but as you will see the core often uses listItem
  • If I need access to the original current tiddler I often use the storyTiddler variable.
  • Rather than pass a value as a parameter, if a variable exists with the correct values just access it as a variable, inside your transcluded tiddler.
    • An example of this is when using the tabs macro, there exists the currentTab

But most of all just design for it;

Try this place the following in a tiddler tagged $:/tags/SideBar

# currentTiddler <<currentTiddler>>
# storyTiddler <<storyTiddler>>
# currentTab <<currentTab>>
# listItem <<listItem>>

Now look at it in the side bar

  • There is no currentTiddler or storyTiddler Set in the sidebar
  • The current tab refers to the tab transcluded
  • The listItem variable was used by $:/core/ui/PageTemplate/sidebar to list the sidebar items, and the particular one here is $:/core/ui/SideBarSegments/tabs which is the missing information, the intermediate tiddler.

Conclusion

I suggest just design for it, similar to the core so the required value is available as a variable throughout you nested transclusions or even procedures/macros.

  • as you did (but don’t use the parameter) use named variable(s)

Thanks, this is useful.

I’m curious why you suggest using a named variable rather than a parameter of the transclusion… Is there a functional difference between, say

<$let var="x">
<$transclude $variable="y"/>
</$let>

as opposed to

<$transclude $variable="y" var="x"/>

I would have thought they were essentially the same

  • No, either are valid

But the first example can also be written

<$let var="x">
 {{y}}
</$let>

Basically because X may come from elsewhere

  • So I think why use a parameter when it is already available as a variable.

eg Y may have been transcluded for each X, so X is available inside Y as <<x>>

<$list filter=" all values" variable=x>

{{y}}

</$list>

The point is that the variable, including the parent tiddlers title, may already be a value inside the transclusion without a named parameter.

That makes sense.

So it is just a case of maintaining flexibility and brevity in the transclusion itself, by defining the variable elsewhere instead of as a parameter.

As an aside: I feel as if every time I understand the logic of wikitext and transclusions on a deeper level, I want to go away and rebuild my existing wikis from the ground-up to take advantage of my new knowledge! :laughing:

I know that feeling. I tend to instead move forward make and learn more things first, because sooner or later I get new ideas and then return to my old work and improve it further.

I did not test your code, but instead of importing the procedure you could make the button-code-tiddler a template itself and transclude the button template with {{||template}} so you can use <<currentTiddler>> inside the template.

It would make the whole code in the template much simpler too.

I’ll run a short test and report back

-mario

1 Like

I did try this at one point but could not get it to work. I think it was because when the default story view template transcludes the button tiddler, the currentTiddler remains set to be the tiddler currently being viewed in the story (which is then referred to using currentTiddler in the action widgets of the default toolbar buttons).

So you could include a line in the button tiddler to update currentTiddler (and then just remember to write your button actions to act on storyTiddler instead), but I’m not sure that’s any tidier than my current solution.

I also had a play around with using the parameters widget, but it seems that even if you specify <$parameters button-tiddler=<<thisTiddler>> $depth="2"> in your button template, the default value for the parameter is still computed within the context of the current transclusion, meaning thisTiddler is still the button template, not the button tiddler itself.

Thinking hypothetically about what would allow me to do what I was attempting (which again, to be clear, isn’t something I need to do, since my current solution works just fine : P):

I think you would effectively need there to be some kind of ..thisTiddler core variable, which would be defined by the transclusion widget as equal to the current value of thisTiddler just before thisTiddler is set during each transclusion, so it becomes equal to the value of thisTiddler one step higher on the tree of transcluded templates.

But I’m not proposing that such a thing be added, since a) I don’t know the first thing about javascript, so couldn’t add it myself, and b) it doesn’t seem like something that would be much use to anyone. But interesting to think about nonetheless.

Perhaps try other variables with the depth other than thisTiddler because thistiddler supposed to return the tiddler it is in. There could be a contradiction in this.

I am yet to explore those extra features they are promising but fiddly.

It’s a bit trickier as I thought at first, because for a toolbar-button the currentTiddler-variable has to be the title of the tiddler, where it is shown. So it can be used as currentTiddler in the button actions.

We need a new variable to tell the template where to get the infos. So the code will be only a little bit different to yours.

Not sure if that’s an improvement.

At least it is an example: How to use a button-template with ViewTemplate toolbar buttons using parametrised transclusions and the “this-tiddler” variable.

If we would “overwrite” the currentTiddler variable, the code would be simpler, but I’m not sure if this would be a disadvantage here.

title: button-template

\whitespace trim
<$parameters source>
<$button actions=<<actions>> tooltip={{{ [<source>get[description]] }}} aria-label={{{ [<source>get[caption]] }}} class=<<tv-config-toolbar-class>> >
  <% if [<tv-config-toolbar-icons>match[yes]] %>
    <$transclude $tiddler={{{ [<source>get[icon]] }}} mode=inline/>
  <% endif %>
  <% if [<tv-config-toolbar-text>match[yes]] %>
    <span class="tc-btn-text"><$text text={{{ [<source>get[caption]] }}}/></span>
  <% endif %>
</$button>

I did use a simple action for button-1 and button-2

title: button-1
tags: $:/tags/ViewToolbar
caption: test-1
description: tooltip for test-1
icon: $:/core/icon

\whitespace trim
\procedure actions()
<$action-navigate $to=test-1/>
\end

<$transclude $tiddler="button-template" source=<<thisTiddler>>/>
title: button-2
tags: $:/tags/ViewToolbar
caption: test-2
description: tooltip for test-2
icon: $:/core/images/mail

\whitespace trim
\procedure actions()
<$action-navigate $to=test-2/>
\end

<$transclude $tiddler="button-template" source=<<thisTiddler>>/>

It did work for me.
have fun!
mario

view-toolbar-buttons-with-template.json (1.3 KB)

Thanks. Yeah, this is pretty similar to what I ended up doing in my original example, except you’ve used a parameterised template tiddler rather than an imported procedure definitions (which is a bit neater in itself).

After the “dance” we done here, I think your initial solution is OK and the “perfectionist” should be “satisfied” :wink: I think there may be more important things to focus on.

Yea, as I wrote. At least it can be used as an example :slight_smile:

-mario

1 Like