$(currentTiddler)$ and <<currentTiddler>> : Macro scope and variable inside macro

55 views
Skip to first unread message

Mohammad

unread,
Feb 8, 2019, 1:42:49 AM2/8/19
to tiddl...@googlegroups.com
Assume you have a macro in a global tiddler e.g


Title: MyMacro
Tag: $:/tags/Macro


\define myMac() <<currentTiddler>>



Then in another tiddler you call the myMac macro as below

<$list filter="[tag[HelloThere]]">
<
<myMac>><br>
</$list>


and testing on Tiddlywiki.com produces

A Gentle Guide to TiddlyWiki
Discover TiddlyWiki
Some of the things you can do with TiddlyWiki
Ten reasons to switch to TiddlyWiki
Examples
What happened to the original TiddlyWiki?

As we learned with the scope of variable and variable inside macro I expect the above syntax fails and the below syntax works

Title: MyMacro
Tag: $:/tags/Macro


\define myMac()  $(currentTiddler)$



As we know a variable from ouside macro should be referenced as $(varname)$.

So what is going on here?

--Mohammad

Eric Shulman

unread,
Feb 8, 2019, 3:05:48 AM2/8/19
to TiddlyWiki
On Thursday, February 7, 2019 at 10:42:49 PM UTC-8, Mohammad wrote:
As we know a variable from ouside macro should be referenced as $(varname)$.

A variable that is defined outside a macro can be referred to within the macro using **either** $(varname)$ or <<varname>>, depending on context.

The difference is that occurrences of $(varname)$ are immediately replaced with the current value of the variable, while <<varname>> is simply returned unchanged as part of the macro output.  Thus these are all valid uses:

\define mymacro() <$somewidget param="$(currentTiddler)$" />
\define mymacro() <$somewidget param="TEXTBEFORE $(currentTiddler)$ TEXTAFTER" />
\define mymacro() <$somewidget param=<<currentTiddler>> />

but the following is NOT (because it mixes <<...>> inside a quoted parameter value where parsing doesn't occur)
\define mymacro() <$somewidget param="TEXTBEFORE <<currentTiddler>> TEXTAFTER" />

-e

TonyM

unread,
Feb 8, 2019, 3:08:00 AM2/8/19
to TiddlyWiki
Mohammad,

I am not so clear on what is wrong, or are you saying nothing is when you expect it to break?

In both cases the "currentTiddler" is being changed (listed) in the List widget. So in both cases the macro will be operating with each value of current tiddler as a result of the list.

As a result both <<currentTiddler>> and $(currentTiddler)$ are the same value. 

I don't think these will vary from each other so often, in the case of currentTiddler which is dynamically generated.

However if you used another variable in the below example you should see what I mean.

\define myMac1()  $(currentTiddler)$
\define myMac2() <<currentTiddler>>
\define myMac3()
<<currentTiddler>><br>
`<<another-variable>>`: <<another-variable>><br> `$ (another-variable) $` $(another-variable)$<br>
<$set name=another-variable value="I have changed another-variable ">
After changing varable `<<another-variable>>`: <<another-variable>><br> `$ (another-variable) $` "$(another-variable)$" but this remains the same<br>
</$set>
\end
\define myMac4()
<<currentTiddler>><br>
`<<another-variable>>`: <<another-variable>><br> `$ (another-variable) $` $(another-variable)$<br>
\end


<$set name=another-variable value="See what I mean">
<$list filter="[tag[HelloThere]]">
currentTiddler: <<currentTiddler>><br>
myMac1: <<myMac1>><br>
myMac2: <<myMac2>><br>
myMac3: <<myMac3>><br>
myMac4: <<myMac4>><br>
<hr>
</
$list>
</$set>

Regards
Tony

TonyM

unread,
Feb 8, 2019, 3:13:32 AM2/8/19
to TiddlyWiki
Post Script,

The ability to compare the current value with its previous can be helpful, lets say you want to do something different when the value changes. Perhaps if you were sorting something by months, and on change in month you want to do something. 

However since TiddlyWiki is really good at processing lists of titles and it removes duplicates in a title list this is only practical when the value comes from elsewhere, such as in a month field on each tiddler.

Regards
Tony

Mohammad

unread,
Feb 8, 2019, 4:24:47 AM2/8/19
to TiddlyWiki
Thanks Eric.
Thank you Tony!

I got the point. I learned in tIddlywiki.com that referring to a variable inside macro is ONLY possible using $(varName)$.
Now I understood both syntax is valid but have different use-case.

Cheers
Mohammad

Mohammad

unread,
Feb 8, 2019, 4:25:02 AM2/8/19
to TiddlyWiki
Added to TW-Scripts.


On Friday, February 8, 2019 at 11:35:48 AM UTC+3:30, Eric Shulman wrote:
Reply all
Reply to author
Forward
0 new messages