Variables are the simplest way to define some wikitext in one place and refer to it in many places.
How variables work
A variable has a name, which is how you refer to the variable, and a value, which is the other piece of wikitext that it stands in for.
As a silly example, let's suppose corporate requires us to include a disclaimer at the end of every paragraph we write:
(This paragraph does not represent the formal opinion of my company.)
We can make this text into a variable using a $set
widget, like this:
<$set
name="disclaimer"
value="(This paragraph does not represent the formal opinion of my company.)"
>
…insert the text of your tiddler here
</$set>
…insert the text of your tiddler here
As you can see in the example above, the $set
widget doesn't itself display anything in the rendered version of your tiddler. However, in between the <$set>
tags, TiddlyWiki will keep track of the fact that the variable with name disclaimer
is set to the stated value. (Once we pass the closing </$set>
tag, TiddlyWiki will forget about the variable. When this happens, we say that the variable has gone out of scope.)
In order to actually use the variable, we put its name between double angle brackets <<>>
in the spot where we want its value to show up; this is called transcluding the variable's value. Let's edit our EmployeeInformationSystem
tiddler to use this disclaimer:
<$set name="disclaimer" value="(This paragraph does not represent the formal opinion of my company.)">
The Employee Information System at this nice company allows employees to perform tasks such as:
* update their names and other personal information (on the front page after signing in)
* view pay stubs ("remuneration" tab)
* request vacation dates ("time off" tab)
<<disclaimer>>
You need to use the Really Annoying Five-Factor Authentication Process to get into the Employee Information System if it is a Tuesday, unless you have also purchased coffee (tea or pastries do not count) in the company cafeteria earlier in the day. <<disclaimer>>
JaneDoe taught me about the EIS in our EmployeeProfileSetupMeeting. <<disclaimer>>
</$set>
The Employee Information System at this nice company allows employees to perform tasks such as:
- update their names and other personal information (on the front page after signing in)
- view pay stubs ("remuneration" tab)
- request vacation dates ("time off" tab)
(This paragraph does not represent the formal opinion of my company.)
You need to use the Really Annoying Five-Factor Authentication Process to get into the Employee Information System if it is a Tuesday, unless you have also purchased coffee (tea or pastries do not count) in the company cafeteria earlier in the day. (This paragraph does not represent the formal opinion of my company.)
JaneDoe taught me about the EIS in our EmployeeProfileSetupMeeting. (This paragraph does not represent the formal opinion of my company.)
Copy this into your version of the tiddler, and you should see the disclaimer appear in the rendered output at each spot where <<disclaimer>>
appears in the wikitext.
The $let
widget
The $set
widget is the most powerful way to set variables, offering a number of different options which you can read about in the documentation. However, the syntax is rather verbose for many purposes – the words name
and value
are clutter, and if you want to set multiple variables, you'll need multiple $set
widgets.
For cases where you don't need any fancy options, the $let
widget allows you to concisely set as many name=value
pairs as you want:
<$let
raffap="Really Annoying Five-Factor Authentication Process"
eis-drink="coffee"
disclaimer="(This paragraph does not represent the formal opinion of my company.)"
>
You need to use the <<raffap>> to get into the EmployeeInformationSystem if it is a Tuesday, unless you have also purchased <<eis-drink>> (tea or pastries do not count) in the company cafeteria earlier in the day. <<disclaimer>>
</$let>
You need to use the Really Annoying Five-Factor Authentication Process to get into the EmployeeInformationSystem if it is a Tuesday, unless you have also purchased coffee (tea or pastries do not count) in the company cafeteria earlier in the day. (This paragraph does not represent the formal opinion of my company.)
We'll primarily use the $let
widget to set variables throughout the rest of the book.
Why use variables?
Eliminating repetition can save typing, which is nice but hardly anything to write home about. Much more importantly, if you want to change a snippet after you create it, you only have to change it in one place. If you only use the snippet a couple of times, changing it in multiple places is no big deal, but there might be some things you end up using dozens or hundreds of times throughout your wiki. And sometimes a full wikitext snippet will be many lines long and easy to mistype, while a variable transclusion is short, concise, and easy to understand.
Additionally, some variables are set automatically by TiddlyWiki, which can allow you to access information you otherwise wouldn't have. For instance, when we created a list widget to show all our meetings in the previous chapter, TiddlyWiki automatically set the <<currentTiddler>>
variable to refer to the item which was currently being processed by the list widget.
In the real world, you will use procedures or field transclusions more often than simple variables when you want to use some wikitext in many places throughout your wiki. But all three of these mechanisms serve the same basic purpose – avoiding repetition – and offer the same benefits discussed here.
Exercises
Suppose that corporate decides it's time to update the disclaimer, and henceforth all documents need to say “doesn't” instead of “does not.” Update the tiddler to make this change. Notice that, with the use of a variable, you need only make the change in one place, even if you included the disclaimer in hundreds of places.
In the EmployeeInformationSystem
tiddler, define a second variable called eis
with the value Employee Information System
, and replace all references to the EIS or Employee Information System with the value of the eis
variable. Make sure the disclaimer still shows up correctly.
Change the EmployeeInformationSystem
tiddler from using two $set
widgets to using one $let
widget. Make sure all references still show up correctly.
What happens if you refer to a variable after the </$set>
or </$let>
?
Consider the following code:
<$set name="variable" value="1">
<<variable>>
<$set name="variable" value="2">
<<variable>>
</$set>
<<variable>>
</$set>
What do you think happens at each transclusion of <<variable>>
? Check your answers by pasting this into a new tiddler.