This is a stripped-down version of a single section of Grok TiddlyWiki, optimized for fast loading and readability by search engines. Some features are missing.

For the full Grok TiddlyWiki experience, please visit the wiki version of this page.

Variables

9th April 2021 at 2:21pm

Variables are a simple 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 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. 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.

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 reference to a variable 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 macros or transclusions more often than 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

Exercise: (s) [Ex:UpdatingDisclaimer]

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.

Exercise: (s) [Ex:EisVariable]

In the tiddler above, 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.

go to answer

Exercise: (s) [Ex:SpacedVariable]

What happens if you put a space in a variable name?

go to answer

Exercise: (s) [Ex:VariableAfterSet]

What happens if you refer to a variable after the </$set>?

go to answer

Exercise: (s) [Ex:NestedSets]

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 reference to <<variable>>? Check your answers by pasting this into a new tiddler.

go to answer

Takeaways

Takeaways are not available in the static version of Grok TiddlyWiki. Visit the wiki version of this page to study takeaways.

↑ 4: Variables, Macros, and Transclusions