Scope of macros

59 views
Skip to first unread message

joearms

unread,
Dec 17, 2018, 7:14:24 AM12/17/18
to TiddlyWikiDev
I was wondering about the scope of macro definitions  ...

The scope of a macro appears to be 'in the current tiddler where it was defined
or any tiddler transcluded from the current tiddler'

Here are two tiddlers

Tiddler1Test is

   \define foo()
   say bar
   \end
   in tiddler 1 foo=<<foo>>

   {{Tiddler2Test}}

Tiddler2Test is

   in tiddler 2 foo = <<foo>>


Tiddler1 renders as
   in tiddler 1 foo=say bar

   in tiddler 2 foo = say bar


Which means the transclusion of Tiddler2 sees the macro - BUT
Tiddler2 viewed at the top level does NOT SEE the macro and renders as

 in tiddler 2 foo =

So how {{Tiddler2Test}} expands is dependent upon the context - so my explanation
of transclusion "it's like a spreadsheet" is not really true. It's like a spreadsheet but including
a cell from different places is not guaranteed to produce the same result.

Up to now, my expectation was that {{X}} always expanded to the same thing until the end
of the universe and not depending upon where it was called from.

Is the moral "don't use macros outside the tiddler where they were defined" or what should I be doing.

I seem to have broken some law of tiddly programming in the "thou shalt not ...." and ended up in the
"there will be gnashing of teeth and gnawing of bones" part of the system.

Cheers

/Joe 





PMario

unread,
Dec 17, 2018, 8:28:03 AM12/17/18
to TiddlyWikiDev
 Hi Joe,

In TW macros are internally handled as variables, similar how the <$set widget defines them. <$set variables only work within <$set>  x  </$set>. macros do have a "tiddler-scope"

If you want to use macros defined in other tiddlers you'll need to import them.


The existing behaviour gives us a simple way to overwrite variable definitions. eg: to create user-defined download buttons. ...

have fun!
mario

joearms

unread,
Dec 17, 2018, 9:03:06 AM12/17/18
to TiddlyWikiDev


On Monday, 17 December 2018 14:28:03 UTC+1, PMario wrote:
 Hi Joe,

In TW macros are internally handled as variables, similar how the <$set widget defines them. <$set variables only work within <$set>  x  </$set>. macros do have a "tiddler-scope"

If you want to use macros defined in other tiddlers you'll need to import them.


I was wondering how to do this.

Thanks

/Joe

Ton Gerner

unread,
Dec 17, 2018, 9:46:54 AM12/17/18
to TiddlyWikiDev
Hi Joe,

Be aware of global macros: macro definitions tagged with $:/tags/Macro

Cheers,

Ton


Simon Huber

unread,
Dec 17, 2018, 3:15:21 PM12/17/18
to TiddlyWikiDev
Hi, the macros tagged $:/tags/Macro are imported in the page-template tiddler ($:/core/ui/PageTemplate) which makes them available within the story river and the sidebar and all sub-transcluded tiddlers...

PageTemplate
    import all $:/tags/Macros
    set some stuff
    transclude all sub-templates (sidebar,story ...)
    close stuff
close PageTemplate


you can import a macro defined in "Macro testtiddler" (which isn't tagged $:/tags/Macro)
using the <$importvariables filter="[[Macro testtiddler]]">
or

\import [[Macro testtiddler]]

in another tiddler
Reply all
Reply to author
Forward
0 new messages