TWiki Variables
Special text strings expand on the fly to display user data or system info
TWikiVariables are text strings -
%VARIABLE%
or
%VARIABLE{ parameter="value" }%
- that expand into content whenever a topic is rendered for viewing. There are two types of variables:
- Preferences variables: Can be defined and changed by the user
- Predefined variables: Defined by the TWiki system or by plugins (for example, the SpreadSheetPlugin introduces a
%CALC{}%
variable)
Using Variables
To use a variable type its name. For example,
- type
%T%
to get (a preferences variable)
- type
%TOPIC%
to get TWikiVariables
(a predefined variable)
- type
%CALC{ "$UPPER(Text)" }%
to get TEXT
(a variable defined by a plugin)
Note:
- To leave a variable unexpanded, precede it with an exclamation point, e.g. type
!%TOPIC%
to get %TOPIC%
- Variables are expanded relative to the topic they are used in, not the topic they are defined in
- Type
%ALLVARIABLES%
to get a full listing of all variables defined for a particular topic
Variable Names
Variable names must start with a letter. The following characters can be letters, numbers and the underscore '_'. You can use both upper-case and lower-case letters and you can mix the characteres. E.g.
%MYVAR%
,
%MyVar%
,
%My2ndVar%
, and
%My_Var%
are all valid variable names. Variables are case sensitive.
%MyVAR%
and
%MYVAR%
are not the same variable.
By convention all settings, predefined variables and variables used by plugins are always UPPER-CASE.
Preferences Variables
Unlike predefined variables, preferences variables can be defined by the user in various places.
Setting Preferences Variables
You can set variables in all the following places:
- system level in TWiki.TWikiPreferences
- plugin topics (see TWikiPlugins)
- local site level in Main.TWikiPreferences
- user level in individual user topics in Main web
- web level in WebPreferences of each web
- topic level in topics in webs
- session variables (if sessions are enabled)
Settings at higher-numbered levels override settings of the same variable at lower numbered levels, unless the variable was included in the setting of FINALPREFERENCES at a lower-numbered level, in which case it is locked at the value it has at that level.
If you are setting a variable and using it in the same topic, note that TWiki reads all the variable settings from the saved version of the topic before it displays anything. This means you can use a variable anywhere in the topic, even if you set it somewhere inconspicuous near the end.
But beware: it also means that if you change the setting of a variable you are using in the same topic,
preview
will show the wrong thing, and you must
save
the topic to see it correctly.
The syntax for setting variables is the same anywhere in TWiki (on its own TWiki bullet line, including nested bullets):
[multiple of 3 spaces] * [space] Set [space] VARIABLENAME [space] = [space] value
Examples:
* Set VARIABLENAME1 = value
* Set VARIABLENAME2 = value
Spaces between the = sign and the value will be ignored. You can split a value over several lines by indenting following lines with spaces - as long as you don't try to use * as the first character on the following line.
Example:
* Set VARIABLENAME = value starts here
and continues here
Whatever you include in your variable will be expanded on display, exactly as if it had been entered directly.
Example: Create a custom logo variable
- To place a logo anywhere in a web by typing
%MYLOGO%
, define the Variable on the web's WebPreferences topic, and upload a logo file, ex: mylogo.gif
. You can upload by attaching the file to WebPreferences, or, to avoid clutter, to any other topic in the same web, e.g. LogoTopic
. Sample variable setting in WebPreferences:
* Set MYLOGO = %PUBURL%/%WEB%/LogoTopic/mylogo.gif
You can also set preferences variables on a topic by clicking the link
Edit topic preference settings
under
More topic actions
. Use the same
* Set VARIABLENAME = value
syntax. Preferences set in this manner are not visible in the topic text, but take effect nevertheless.
Parameterized Variables (Macros)
It is possible to pass parameters to TWiki variables. This is called a macro in a programming language.
To define a parameterized variable, set a variable that contains other variables, such as:
* Set EXAMPLE = Example variable using %DEFAULT%, %PARAM1% and %PARAM2%
* Set DEMO = Demo using %DEFAULT{ default="(undefined)" }%,
%PARAM1{ default="(undefined)" }% and %PARAM2{ default="(undefined)" }%
A special
%DEFAULT%
variable denotes the default (nameless) parameter of the calling variable. Variables optionally may list a
default="..."
parameter that gets used in case the calling variable does not specify that parameter.
To use a parameterized variable (or call a macro), add parameters within the curly brackets, such as:
* %EXAMPLE{ "foo" PARAM1="bar" PARAM2="baz" }%
* %DEMO{ "demo" PARAM2="parameter 2" }% -- note that PARAM1 is missing
which resolves to:
- %EXAMPLE{ "foo" PARAM1="bar" PARAM2="baz" }%
- %DEMO{ "demo" PARAM2="parameter 2" }% -- note that PARAM1 is missing
Parameters in the variable definition are expanded using the following sequence:
- Parameter from variable call. In above example,
%PARAM1%
gets expanded to bar
.
- Session variable and preferences settings
Example
Define variables:
* Set DRINK = red wine
* Set FAVORITE = My %DEFAULT{default="favorite"}% dish is %DISH{default="steak"}%,
my %DEFAULT{default="favorite"}% drink is %DRINK%.
The default can be defined with a default parameter (
%DISH{default="steak"}%
), or as a preferences setting (
Set DRINK = ...
).
Use Variables:
%FAVORITE{ DISH="Sushi" DRINK="Sake" }%
Returns:
%FAVORITE{ DISH="Sushi" DRINK="Sake" }%
%FAVORITE{}%
Returns:
%FAVORITE{}%
%FAVORITE{ "preferred" }%
Returns:
%FAVORITE{ "preferred" }%
Access Control Variables
These are special types of preferences variables to control access to content.
TWikiAccessControl explains these security settings in detail.
Local values for variables
Certain topics (a users home topic, web site and default preferences topics) have a problem; variables defined in those topics can have two meanings. For example, consider a user topic. A user may want to use a double-height edit box when they are editing their home topic - but
only when editing their home topic. The rest of the time, they want to have a normal edit box. This separation is achieved using
Local
in place of
Set
in the variable definition. For example, if the user sets the following in their home topic:
* Set EDITBOXHEIGHT = 10
* Local EDITBOXHEIGHT = 20
Then when they are editing any other topic, they will get a 10 high edit box. However when they are editing their home topic, they will get a 20 high edit box.
Local
can be used wherever a preference needs to take a different value depending on where the current operation is being performed.
Use this powerful feature with great care!
%ALLVARIABLES%
can be used to get a listing of the values of all variables in their evaluation order, so you can see variable scope if you get confused.
Frequently Used Preferences Variables
The following preferences variables are frequently used. They are defined in
TWikiPreferences#Miscellaneous_Settings:
-
%BB%
- line break and bullet combined
-
%BB2%
- level 2 bullet with line break
-
%BB3%
- level 3 bullet with line break
-
%BB4%
- level 4 bullet with line break
-
%BR%
- line break
-
%BULLET%
- bullet sign
-
%CARET%
- caret symbol
-
%VBAR%
- vertical bar
-
%H%
- Help icon
-
%I%
- Idea icon
-
%M%
- Moved to icon
-
%N%
- New icon
-
%P%
- Refactor icon
-
%Q%
- Question icon
-
%S%
- Pick icon
-
%T%
- Tip icon
-
%U%
- Updated icon
-
%X%
- Alert icon
-
%Y%
- Done icon
-
%RED% text %ENDCOLOR%
- colored text (also %YELLOW%
, %ORANGE%
, %PINK%
, %PURPLE%
, %TEAL%
, %NAVY%
, %BLUE%
, %AQUA%
, %LIME%
, %GREEN%
, %OLIVE%
, %MAROON%
, %BROWN%
, %BLACK%
, %GRAY%
, %SILVER%
, %WHITE%
)
-
%REDBG% text %ENDBG%
- colored background (also %YELLOWBG%
, %ORANGEBG%
, %PINKBG%
, %PURPLEBG%
, %TEALBG%
, %NAVYBG%
, %BLUEBG%
, %AQUABG%
, %LIMEBG%
, %GREENBG%
, %OLIVEBG%
, %MAROONBG%
, %BROWNBG%
, %BLACKBG%
, %GRAYBG%
, %SILVERBG%
, %WHITEBG%
)
There are additional useful preferences variables defined in
TWikiPreferences, in
Main.TWikiPreferences, and in
WebPreferences of every web.
Predefined Variables
Most predefined variables return values that were either set in the configuration when TWiki was installed, or taken from server info (such as current username, or date and time). Some, like
%SEARCH%
, are powerful and general tools.
- Predefined variables can be overridden by preferences variables (except TOPIC and WEB)
- Plugins may extend the set of predefined variables (see individual plugin topics for details)
- Take the time to thoroughly read through ALL preference variables. If you actively configure your site, review variables periodically. They cover a wide range of functions, and it can be easy to miss the one perfect variable for something you have in mind. For example, see
%INCLUDINGTOPIC%
, %INCLUDE%
, and the mighty %SEARCH%
.
Search predefined variables
List of all predefined variables
This TWiki: - TWiki-6.1.0, Mon, 16 Jul 2018, build 30610