Use %SET{}%
to store arbitrary text in a named variable, and reuse it with %GET{}%
later on within the topic or an included topics. By default, variables live only during topic rendering time, e.g. they do not persist between topic views. It is also possible to remember the variables between topic views, however they are not version controlled like other content in TWiki.
%SET{}%
and %GET{}%
can be nested inside other TWiki variables and get handled as expected, e.g. inside out, and left to right.
Alternatives to this plugin are:
%CALC{$SET(some_name, anything)}%
and retrieved with $GET()
. These variables persist during page rendering time. A limitation of %CALC{}
is that it does not follow the standard TWiki variable evaluation order (nesting inside out, left to right).
%GET{}%
. No output is shown, e.g. %SET{}%
resolves to an empty string.
%SET{ "name" value="..." remember="1" }%
Parameter | Description | Default |
---|---|---|
"name" | Name of variable. Alphanumeric characters, dashes and underscores can be used. | (required) |
value="..." | Value of variable. Escape double quotes with backslash. | (required, may be empty) |
remember="1" | If set, the variable will be stored persistently so that it can be used later in any TWiki topic. See important notes. | "0" |
%SET{"lunch" value="Sushi"}%
Important notes when using the remember option:
"campaign"
, use a more descriptive name "Marketing-Europe-2025-Campaign"
.
%SET{}%
.
%GET{ "name" default="..." }%
Parameter | Description | Default |
---|---|---|
"name" | Name of variable. | (required) |
format="..." | Format with supported variables: • $name for variable name • $value for variable value • $isdefined expanding to 1 or 0 depending if variable is defined or not • $isset expanding to 1 or 0 depending if variable is logically true or false • $ispersistent expanding to 1 or 0 depending if variable is persistent or not • all FormatTokens such as $dollar , $n , $percnt . | "$value" |
default="..." | Text shown if variable is not defined, e.g. not found. This parameter overrides the format parameter. | "" (empty string) |
%GET{"lunch"}%
returns Sushi
if the following has been previously set:%SET{ "lunch" value="Sushi" }%
%SET{}%
.
%SETGETDUMP{ format="..." separator="..." }%
Parameter | Description | Default |
---|---|---|
format="..." | Format output using variables $name and $value | "name: $name, value: $value <br />" |
separator="..." | String used for separating entries | "\n" |
%SETGETDUMP{"| $name | $value |" separator="$n"}%
A variable can be set and used several times.
Raw text:
* Set "demo" to "uno": %SET{ "demo" value="uno" }% * Get "demo": %GET{ "demo" }% * Set "demo" to "due": %SET{ "demo" value="due" }% * Get "demo": %GET{ "demo" }% * Set "demo" to "tre": %SET{ "demo" value="tre" }% * Get "demo": %GET{ "demo" }%
|
Rendered text:
|
A SEARCH result can be assigned to a variable for later use. This can be useful for performance reasons if you need the result multiple times. The result can also be post-processed, such as with a $LISTMAP()
of the SpreadSheetPlugin.
Raw text:
%SET{ "result" value="%SEARCH{ "." topic="*Skin" nonoise="on" format="$topic" separator=", " }%" }% * Result: %GET{result}% * Again: %GET{result}%
|
Rendered text:
|
In a topic, set a variable with the remember flag set that stores the current time. Add also a GET before the SET, it will show the time the topic has last been viewed.
Raw text:
* Last view: %GET{ "SetGetPlugin-lastview" }% * Now: %SET{ "SetGetPlugin-lastview" remember="1" value="%SERVERTIME{$year-$mo-$day $hou:$min:$sec}%" }% %GET{ "SetGetPlugin-lastview" }%
|
Rendered text:
|
This example shows how you can remember the mood of users. The form shows a picklist to select a mood. The mood is stored persistently per user, and shown.
Raw text:
%IF{ "defined 'mood'" then="$percntSET{ \"SetGetPlugin-mood-%WIKINAME%\" remember=\"1\" value=\"%URLPARAM{mood}%\" }$percnt" }% My current mood: %GET{ "SetGetPlugin-mood-%WIKINAME%" }% for %WIKIUSERNAME% <form action="%SCRIPTURL{view}%/%WEB%/%TOPIC%#MyMood"> Change my mood: <select name="mood"> <option>%URLPARAM{mood}%</option> <option>:-D</option> <option>:-)</option> <option>:-I</option> <option>:-(</option> <option>:mad:</option> </select> <input type="submit" value="Set" class="twikiSubmit" /> </form>
|
Rendered text:
My current mood: for TWikiGuest
|
Variables can also be set and retrieved by invoking a REST (REpresentational State Transfer) request on the TWiki server using the rest script. To persistently remember the state of interactive browser-based JavaScript applications, you can set and get variables using this REST interface via Ajax calls.
The rest script requires authentication, e.g. the user agent is prompted to authenticate if needed.
To set a variable call %SCRIPTURL{rest}%/SetGetPlugin/set
. It accepts the following URL parameters:
name
- name of variable (required)
value
- value of variable (required, may be empty)
remember
- persistently remember if set to 1 (optional)
To get a variable call %SCRIPTURL{rest}%/SetGetPlugin/get
. It accepts the following URL parameters:
name
- name of variable (required)
default
- default returned if variable is not found (optional)
%<plugin>_<setting>%
, i.e. %NEWPLUGIN_SHORTDESCRIPTION%
data/debug.txt
)
This plugin is pre-installed. TWiki administrators can upgrade the plugin as needed on the TWiki server.
SetGetPlugin.zip
in your twiki installation directory. Content: File: | Description: |
---|---|
data/TWiki/SetGetPlugin.txt | Plugin topic |
data/TWiki/VarGET.txt | GET documentation |
data/TWiki/VarSET.txt | SET documentation |
data/TWiki/VarSETGETDUMP.txt | SETGETDUMP documentation |
lib/TWiki/Plugins/SetGetPlugin.pm | Plugin Perl module |
lib/TWiki/Plugins/SetGetPlugin/Core.pm | Core Perl module |
Result: TWiki rocks!
Plugin Author: | TWiki:Main.PeterThoeny![]() |
Copyright: | © 2010-2013 TWiki:Main.PeterThoeny![]() © 2010-2013 TWiki:TWiki.TWikiContributor ![]() |
License: | GPL (GNU General Public Licen:se![]() |
Plugin Version: | 2013-01-28 |
2013-01-28: | TWikibug:Item7091![]() |
2012-11-12: | TWikibug:Item7032![]() |
2012-11-12: | TWikibug:Item7020![]() |
2012-10-11: | TWikibug:Item6978![]() ![]() |
2012-09-25: | TWikibug:Item6943![]() |
2012-01-06: | TWikibug:Item6828![]() ![]() |
2011-07-09: | TWikibug:Item6725![]() |
2011-04-04: | TWikibug:Item6638![]() |
2011-03-26: | TWikibug:Item6670![]() |
2010-10-17: | TWikibug:Item6597![]() |
TWiki Dependency: | $TWiki::Plugins::VERSION 1.1 |
CPAN Dependencies: | none |
Other Dependencies: | none |
Perl Version: | 5.005 |
Benchmarks![]() |
GoodStyle 100%, FormattedSearch 99%, SetGetPlugin 99% |
Plugin Home: | http://TWiki.org/cgi-bin/view/Plugins/SetGetPlugin![]() |
Feedback: | http://TWiki.org/cgi-bin/view/Plugins/SetGetPluginDev![]() |
Appraisal: | http://TWiki.org/cgi-bin/view/Plugins/SetGetPluginAppraisal![]() |
Related Topics: VarSET, VarGET, TWikiPlugins, DeveloperDocumentationCategory, AdminDocumentationCategory, TWikiPreferences, SpreadSheetPlugin