Can a Macro access a filed in its own Tiddler (ex. get the title of its own tiddler?)

122 views
Skip to first unread message

Mohamed Amin

unread,
May 21, 2021, 8:29:13 PM5/21/21
to TiddlyWiki
Hi,

As I know, using "currentTiddler" inside a Macro will get the "Caller Tiddler" title.
Is there a way to let a Macro to access its own tiddler?

For example: 
If I've a Macro name "myMacro01" which is defined in a tiddler called "Macro01_Tdlr".
Now, if I call this macro from a tiddler called "XYZ" , then the "currentTiddler" value will be "XYZ".
Is there a way to let the macro get its own tiddler name (Macro01_Tdlr)?

A use case: 
I'm thinking to have a series of Macros, each is defined in its own Tiddler.
then I'll use the fields in each "Macro Tiddler" to control the behavior of the macro (the Macro will need to access the fields inside its own tiddler) 

any idea?

Thanks in advance

TW Tones

unread,
May 21, 2021, 10:08:52 PM5/21/21
to TiddlyWiki
Mohamed,

The easiest way is to set values in the macro itself.

eg in a tiddler "MacroDef
\define themacroname()
<$set  name=macro-name value="themaconame">
<$set  name=macro-tiddler value="MacroDef">
  <$link /> <<macxro-name>> in <<macro-tiddler>><br>
 Do macro stuff here
</$set></$set>
\end



Then in another tiddler
<<themacroname>>

So within the macro its name and tiddler are available as a variable.

Alternatively; the following method makes use of macro parameters;

\define themacroname(macro-name:"themacroname" macro-tiddler:"MacroDef" )
  <$link /> <<__macro-name__>> in <<__macro-tiddler__>><br>
  <hr><!-- OR -->
  <$link /> $macro-name$ in $macro-tiddler$ <br>
 Do macro stuff here
\end

  • <<__macroParameter__>> Parameter-as-variable access to a parameter defined in the macro parameters list as it it were a variable.
  • This method also Allows the $parameter$ substitution that allows you to do things like tooltip="My  $parameter$" in a button, which is a form of concatenation.

Notes
  • There are other special ways available to identify a tiddler that has being transcluded, but the transclude tiddler can also set set a variable or two.
  • This will cover most requirements except where the macro is itself generated automatically, ie when putting in static values is insufficient.
  • Another trick is a macro that you give a macroname as its parameter, that it then uses to call the macro, thus the macro name is available.
  • Given a macroname you can also search for "\define macroname(" in all tiddlers to find one or more places where it is defined.
Tones

Mohamed Amin

unread,
May 22, 2021, 9:33:46 AM5/22/21
to TiddlyWiki
Thanks a lot TW Tones for your detailed answer, it's really helpful and thoughtful.

BUT you already opened my eyes on VERY interesting points :
  1. Identify a tiddler that has being transcluded
  2.  The macro is itself generated automatically
Appreciate your help to know where to start learning about those points.

Thanks again

TW Tones

unread,
May 22, 2021, 9:06:53 PM5/22/21
to TiddlyWiki
Mohamed,

Perhaps I can publish something another time to go into these two things in the future. However I will once again give some essential information to set you on the path. 
Most of these things have being covered in the past in the forum however they would be hard to find, 
  1. Identify a tiddler that has being transcluded see attached 
  2. There are many ways to show this happening all you need you do is have something that generates a tiddler containing a macro definition and tag it $:/tags/Macro, eg a button can create such a tiddler.
Regards
Tones


Transclusion Variable.json

Mohamed Amin

unread,
May 25, 2021, 8:15:42 PM5/25/21
to TiddlyWiki

Appreciate your support TW Tones , I think I'll need sometime to get my head around this "Transclusion Monster" :)

Thanks again

TW Tones

unread,
May 25, 2021, 8:55:34 PM5/25/21
to TiddlyWiki
Mohamad,

I can assure you it is worth it. Remember you can always hard code values in you tiddlers that name the tiddler. My documentation in the attached is a wikitext method to do it programaticaly but in many ways it is advanced tiddlywiki.

best of luck

Reply all
Reply to author
Forward
0 new messages