TIP +, Access the CodeTiddler Title you Transcluded or the macro is defined in

52 views
Skip to first unread message

TW Tones

unread,
Jun 24, 2020, 8:37:17 PM6/24/20
to TiddlyWiki

Folks,


This solution may make sense more to experienced TiddlyWiki users who make their own macros, or templates they transclude from elsewhere. 
  • Which is possibly all slightly advanced users. 
  • For me personally, this is a revolutionary advance for coding, I wonder if anyone else will think so?
Its a working solution you can adopt now, However I raised this issue in github because its just a small code change with a lot of potential. 
  1. Please support this request with a thumbs up if you support this.
  2. However if you like this and can help convert this to a PR Pull Request I would appreciate it.
  3. A key desire is not needing to wikify the variable.
Summary: Provision of a global variable codeTiddler that returns the tiddler title that contains the code that is in use. Testing suggests this worked with multiple transclusions deep.

Overview
  • When writing macros or templates that other tiddlers we call or transclude, we tend to do so with the currentTiddler set so the macro or transclusion acts on the currentTiddler.
    • eg `{{||transcludedTiddler}}`
  • This is a good design strategy because it is then easy to use the same macro or transclusion on each item in a list as long as currentTiddler is set to the desired title.
  • So if you want to reference fields in the codeTiddler, the one containing the macros or transclusion template, you are forced to encode full text references eg `{{codeTiddlername!!codefieldname}}`
    • This makes it complex to rename, clone or generate code tiddlers because you are forced to edit the text in multiple places to update all text references.
    • This gets in the way of my autonomous field and autonomous tag idea because it stops you simply cloning to create a new one.
  • It opens other possibilities as well (see below)
  • The solution seemed impossible until I discovered a trick the `<<transclusion>>` system variable see how it includes "the title of the tiddler being transcluded"
Why?
  • Access to a new variable that can provide a link to actual code tiddlers transcluded or there because of a global macro definition.
    • This could make some uses of references and back-links obsolete.
  • Comparing the codeTiddler variable with the currentTiddler Variable allows you;
    • To have something appear only when opening the code tiddler in the story, but not when the same tiddler is transcluded, help with documentation and settings.
    • Detect at render time the name of the codeTiddler and use this in code (Eg reference code tiddler fields)
  • Without a currentTiddler (no value) and with a codeTiddler indicates the tiddler is being rendered in the side bar or elsewhere outside the story
    • You can then test if the tiddler has the sidebar tag and write code that displays only or not in the sidebar.
    • Applies to other locations above and below story, top left etc...
  • Multiple level transclusions or macros can be set to display the tiddler they are defined in, to help debugging
Example

This example placed in a codeTiddler or template that is transcluded, it determines if you are viewing the tiddler in the story or via a transclusion
and displays when transcluded the field in itself, not the currentTiddler. 
<$wikify name=code-tiddler text="""<<codeTiddler>>""">
<$list filter="[all[current]match
<code-tiddler>]">
  In Code tiddler <$view field=fieldname/>
</$list>
<$list filter="[all[current]!match
<code-tiddler>]"variable=nul>
  Transcluded In Calling tiddler <$view field=fieldname/>
</$list>
</$wikify>
The Solution
A Simple Macro tiddler with $:/tags/macro will do it

\define pipe() |
\define codeTiddler() {{{ [<transclusion>split<pipe>nth[2]] }}}
The first macro defines the pipe symbol so it can be used in the split operator codeTiddler uses the 2nd title found as a result of the
transclusion macro `{currentTiddler|codeTiddler|||}` The first value is equal to the currentTiddler, so rather than access that we can just make use of the existing currentTiddler Variable


Reply all
Reply to author
Forward
0 new messages