[TW5] Is it possible to create global variable just like global macro?

498 views
Skip to first unread message

Penguinlay

unread,
Jan 20, 2017, 4:36:42 AM1/20/17
to TiddlyWiki
I learn that macros and variables are two sides of the same coins but I don't know how to create a global variable just like global macro.

I imitate the global macro practice with $set as well as $var but both does not work.

So, can you give me example if it is doable?

Jed Carty

unread,
Jan 20, 2017, 5:06:19 AM1/20/17
to TiddlyWiki
You can make it exactly the same way as a global macro, put

\define VariableName() VariableValue

into a tiddler with the tag $:/tags/Macro and anywhere you put <<VariableName>> you will get  the variable. You can use it in filters like this:

<$list filter='[tag<VariableName>]'/>

and inside macros you can refer to the variable like this, but this only works inside macros:

\define SomeMacro()
The value of the variable VariableName is $(VariableName)$
\end

Hopefully that answers your question.

Penguinlay

unread,
Jan 20, 2017, 7:42:44 PM1/20/17
to TiddlyWiki
Thank you for the reply.
 
You can make it exactly the same way as a global macro, put
\define VariableName() VariableValue
into a tiddler with the tag $:/tags/Macro and anywhere you put <<VariableName>> you will get  the variable.

This global variable does not work at all in other tiddlers.
Once, I put a new line with <<variableName>> in the original variable tiddler, it works if I do <<variableValue>> in other tiddlers.

However,

You can use it in filters like this:
<$list filter='[tag<VariableName>]'/>
This does not work at all whether I put that extra code in variable tiddler.


It doesn't work inside macro either.

I don't know what is the problem.

Penguinlay

unread,
Jan 20, 2017, 11:49:47 PM1/20/17
to tiddl...@googlegroups.com
The steps I took are:

Step 1: I created a global variable tiddler as suggested.


Step 2: I saved the above tiddler and created a new tiddler accessing the above global macro.


Step 3: I saved the second tiddler, and it didn't display the variable value.


Step 4: I edited the global variable tiddler by adding

<<variableName>>
as the second line of code.


Step 5: I saved the global variable tiddler and the second tiddler worked correctly then.

Jed Carty

unread,
Jan 21, 2017, 3:48:14 AM1/21/17
to TiddlyWiki

It looks like you do need a line break after the macro definition, I wasn't aware of that. All three cases worked for me, see screenshots below. Make sure you actually save the macro tiddler otherwise I don't think that the global macro takes effect. So both of the tiddlers in the screenshots here have been saved normally, I just opened them up to edit to show the contents.

Tobias Beer

unread,
Jan 21, 2017, 4:59:42 AM1/21/17
to TiddlyWiki
Hi Penguinlay,
 
This global variable does not work at all in other tiddlers.
Once, I put a new line with <<variableName>> in the original variable tiddler, it works if I do <<variableValue>> in other tiddlers.

It's already taken care of. I f you try it on prerelease it already works:

Here's the ticket for the bug & fix:

#2076 Bug: global single-line macro definition

Until next release, you need to put the macro definition
for global macros on its own line.

Best wishes,

Tobias.

Penguinlay

unread,
Jan 21, 2017, 6:38:56 AM1/21/17
to TiddlyWiki
Thank you, both Jed and Tobias. I will just use the full macro definition format instead of one line definition for now.

I get one more question with global variable (macro).

For global variable, the value is passed as a string if I understood correctly.
So, every time I call that variable, the value is also parse as string. It means I can't set filters as values and expect them to work as filters, right?

I mean let's say the value of my global variable variableName is prefix[=], then when I put it in filter as <$list filter = "[<variableName>]"/>, it won't work. It won't work as the perimeter of a macro as well.

The reason I am asking this is because I want to set global variable for different combinations of filters, like admin tiddlers, foo tiddlers with custom features, etc., that I use often.

Jed Carty

unread,
Jan 21, 2017, 10:55:22 AM1/21/17
to TiddlyWiki
When you use < and > in a filter it passes whatever the variable is as an argument, so you using [<somemacro>] would use the output of <<somemacro>> as a tiddler title. To pass it as a string the way you are using you need to use $(somemacro)$ instead. This only works if it is inside another macro and it will put in the raw value returned by the macro. So in your case using 

<$list filter='[$(variableName)$]'/> should be the same as <$list filter='[prefix[=]]'/>

I promise there is a way that all of this makes sense, it just isn't always easy to see or explain.

Penguinlay

unread,
Jan 21, 2017, 9:09:41 PM1/21/17
to TiddlyWiki
So in your case using 

<$list filter='[$(variableName)$]'/> should be the same as <$list filter='[prefix[=]]'/>


It doesn't work saying missing [ in filter expression. Here are my trials.






Jed Carty

unread,
Jan 22, 2017, 4:12:50 AM1/22/17
to TiddlyWiki
As I said, it will only work inside a macro because it uses the $(foo)$ syntax, so you have to put your list in a macro.

\define thisMacro()
<$list filter='[$(test)$]'>


</$list>
\end


<<thisMacro>>

Penguinlay

unread,
Jan 22, 2017, 1:32:20 PM1/22/17
to TiddlyWiki
Okay, got it. Thank you! Not what I expected but I understand now.
Reply all
Reply to author
Forward
0 new messages