Problem with var in macro

58 views
Skip to first unread message

Jorge Benítez López

unread,
Jun 17, 2021, 8:45:48 AM6/17/21
to TiddlyWiki
Hello,

I am new in this group. First of all, congratulations and thanks for the work in Tiddlywiki

I have a problem with var in Macro. The first example work but the second no. ¿why?:

1 example:

\define countTag(tag:picture)
$tag$  <$list filter="[tag[$tag$]count[0]]"/>
\end

<<countTag>>

Result:  picture 2



2 example:

\define countTag2()
<<tag2>>: <$list filter="[tag[<<tag2>>]count[0]]"/>
\end

<$set name=tag2 value=picture>
<<countTag2>>
</$set>


Result:  picture 0



I have 2 tiddlys with the tag picture. Why the result is different????

Thanks

Eric Shulman

unread,
Jun 17, 2021, 10:02:46 AM6/17/21
to TiddlyWiki
On Thursday, June 17, 2021 at 5:45:48 AM UTC-7 jorgeben...@gmail.com wrote:
\define countTag2()
<<tag2>>: <$list filter="[tag[<<tag2>>]count[0]]"/>
\end

<$set name=tag2 value=picture>
<<countTag2>>
</$set>

Result:  picture 0
I have 2 tiddlys with the tag picture. Why the result is different????

The difference is that when specifying a filter operand, you use square brackets only with literal operand values. Thus, in the first macro, because the $tag$ is a macro parameter that is automatically substituted into the filter syntax by the TWCore macro processor, you  correctly wrote tag[$tag$].  However, in the second macro, tag2 is a variable that is referenced by the tag filter and, when using a variable as the operand of a filter operator, you use single angle brackets to surround a variable operand reference, instead of the square brackets you used previously for the literal $tag$ operand value.  Thus, you need to use: tag<tag2> in your filter syntax.

Your second macro should be written as:
\define countTag2()
<<tag2>>: <$list filter="[tag<tag2>count[0]]"/>
\end

enjoy,
-e

TW Tones

unread,
Jun 20, 2021, 8:19:10 PM6/20/21
to TiddlyWiki
This may also be a good opportunity to point out in macros you can also access the content of a variable defined outside the macro, as a substitution using the form $(varname)$ eg $(tag2)$

Tones

Reply all
Reply to author
Forward
0 new messages