Home | Gifts |
%SET{}%
to store a JSON object or arbitrary text in a named variable, and reuse it with %GET{}%
later on within the topic or an included topic. By default, variables live only during topic rendering time, e.g. they do not persist between topic views. It is also possible to make variables persist, e.g. to remember them between topic views.
%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:
%CALCULATE{$SET(some_name, any value)}%
and retrieved with $GET()
. These variables persist during page rendering time.
%GET{}%
. No output is shown, e.g. %SET{}%
resolves to an empty string. It is also possible to set a JSON object using a JSON path.
%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. Alternatively use the store parameter. See important notes. | "0" |
store="..." | Specify a store name to persistently store the variable, such as store="Parts" . Use alphanumeric characters, dashes and underscores for the name. For better performance, store is preferred over the remember parameter if you need to store a large dataset. See important notes. | "" |
%SET{"lunch" value="Sushi"}%
%SET{ name = { ... } remember="1" }%
remember="1"
or store="..."
parameter can be appended. If specified, the JSON object will be stored persistently so that it can be used later in any TWiki topic.
%SET{ menu = { "File": { "New": [ "new", "F" ], "Open": [ "open", "F" ] }, "Edit": { "Copy": [ "cpy", "F" ], "Paste": [ "pst", "F" ] } } }%
- set a JSON object
%GET{ menu }%
- returns: {"File":{"New":["new","F"],"Open":["open","F"]},"Edit":{"Copy":["cpy","F"],"Paste":["pst","F"]}}
%SET{ menu.File.Open[1] = "T" }%
- modify a JSON object
%GET{ menu }%
- returns: {"File":{"New":["new","F"],"Open":["open","T"]},"Edit":{"Copy":["cpy","F"],"Paste":["pst","F"]}}
%SET{ menu.Edit.Cut = [ "cut", "T" ] }%
- add to a JSON object
%GET{ menu }%
- returns: {"File":{"New":["new","F"],"Open":["open","T"]},"Edit":{"Copy":["cpy","F"],"Paste":["pst","F"],"Cut":["cut","T"]}}
"campaign"
, use a more descriptive name "Marketing-Europe-2024-Campaign"
.
%SET{}%
.
%GET{ "name" default="..." }%
Parameter | Description | Default |
---|---|---|
"name" | Name of variable, such as menu . May optionally contain a JSON path, such as menu.File.Open . | (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) |
store="..." | Specify a store name that holds the persistent variable. This assumes the variable was previously set with the same store name. | "" |
%GET{"lunch"}%
returns Sushi
if the following has been previously set:%SET{ "lunch" value="Sushi" }%
%GET{ name }%
%SET{ menu = { "File": { "New": [ "new", "F" ], "Open": [ "open", "F" ] }, "Edit": { "Copy": [ "cpy", "F" ], "Paste": [ "pst", "F" ] } } }%
- set a JSON object
%GET{ menu.File.Open }%
- returns: ["open","F"]
%SET{}%
.
%SETGETDUMP{ remember="1" format="..." separator="..." }%
Parameter | Description | Default |
---|---|---|
remember="1" | Dump all persistent variables | (volatile variables) |
store="..." | Dump variables of a specific store | (volatile variables) |
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"}%
{ "key1": "value1", "key2": "value2", ... }
- hash with key/value pairs; the value can be another JSON object
[ "va1", "val2", "val3", ... ]
- array with values; the value can be another JSON object
"String"
- text string enclosed in double quotes
%SET{ name = object }%
or %SET{ name.path = object }%
.
name
- variable name, such as: menu
.path
- JSON path, such as: .File.Open[1]
in menu.File.Open[1]
.hashName
, such as: .File
[n]
, such as: [1]
object
- JSON object, simple (hash, array, string) or nested, such as: { "Breakfast": "cereal", "Lunch": "salad", "Dinner": "Sushi" }
- hash with key/value pairs; the value can be another JSON object
[ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ]
- array with values; the value can be another JSON object
"Sushi"
- text string enclosed in double quotes
ERROR:
An optional remember="1"
or store="some_name"
parameter can be added. Use one or the other, not both. If specified, the JSON object will be stored persistently so that it can be used later in any TWiki topic. The remember="1"
option stores the JSON object in the default store; with store="..."
you can specify a store name to save the JSON object.
%GET{ name }%
to get the full object, or %GET{ name.path }%
to get a sub-part of the object using a JSON path.
name
- variable name, such as: menu
.path
- JSON path, such as: .File.Open[1]
in menu.File.Open[1]
.hashName
, such as: .File
[n]
, such as: [1]
[0]...[9999]
and asterisk [*]
[*]
asterisk wildcard to indicate all array items (only one wildcard can be used in a JSON path)
{"key1":"value1","key2":"value2"}
- hash with key/value pairs; space trimmed; nondeterministic sequence; the value can be another JSON object
["va1","val2","val3"]
- array with values; space trimmed; the value can be another JSON object
String
- text string, without enclosing double quotes
%SET{ menu = { "File": { "New": [ "new", "F" ], "Open": [ "open", "F" ] }, "Edit": { "Copy": [ "cpy", "F" ], "Paste": [ "pst", "F" ] } } }%
- set a JSON object
%GET{ menu }%
- get the JSON object, returns: {"File":{"New":["new","F"],"Open":["open","F"]},"Edit":{"Copy":["cpy","F"],"Paste":["pst","F"]}}
%GET{ menu.File.Open }%
- get a JSON object using JSON path, returns: ["open","F"]
%SET{ menu.File.Open[1] = "T" }%
- modify a JSON object
%GET{ menu }%
- get the JSON object, returns: {"File":{"New":["new","F"],"Open":["open","T"]},"Edit":{"Copy":["cpy","F"],"Paste":["pst","F"]}}
%SET{ menu.Edit.Cut = [ "cut", "T" ] }%
- add to a JSON object
%GET{ menu }%
- get the JSON object, returns: {"File":{"New":["new","F"],"Open":["open","T"]},"Edit":{"Copy":["cpy","F"],"Paste":["pst","F"],"Cut":["cut","T"]}}
[*]
wildcard:
%SET{ projects = [ { "name": "Prj A", "key": "PrjA", "owner": { "name": "Aaron", "id": 1 } }, { "name": "Prj B", "key": "PrjB", "owner": { "name": "Boris", "id": 2 } }, { "name": "Prj C", "key": "PrjC", "owner": { "name": "Charlie", "id": 3 } } ] }%
- set a JSON array of hashes object
%GET{ projects[1] }%
- get the second array item of the JSON object using JSON path, returns: {"owner":{"name":"Boris"},"name":"Prj B","key":"PrjB"}
%GET{ projects[1].name }%
- get the project name of the second array item, returns: Prj B
%GET{ projects[*].name }%
- use wildcard to get all project names, returns: ["Prj A","Prj B","Prj C"]
%GET{ projects[*].owner }%
- use wildcard to get all project owner objects, returns: [{"name":"Aaron"},{"name":"Boris"},{"name":"Charlie"}]
%GET{ projects[*].owner.name }%
- use wildcard to get all project owner names, returns: ["Aaron","Boris","Charlie"]
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:
|
$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:
|
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:
|
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 |
%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)
store
- specify a store name to persistently store the variable (optional)
%SCRIPTURL{rest}%/SetGetPlugin/get
. It accepts the following URL parameters: name
- name of variable (required)
default
- default returned if variable is not found (optional)
store
- specify a store name that holds a persistent variable (optional)
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 |
lib/TWiki/Plugins/SetGetPlugin/Config.spec | Configuration file |
Result: TWiki rocks!
Plugin Author: | TWiki:Main.PeterThoeny |
Copyright: | © 2015 Alba Power Quality Solutions © 2015 Wave Systems Corp. © 2010-2018 TWiki:Main.PeterThoeny © 2010-2018 TWiki:TWiki.TWikiContributor |
License: | GPL (GNU General Public License) |
Sponsor: | Wave Systems Corp. |
Plugin Version: | 2018-07-05 |
2018-07-05: | TWikibug:Item7841: Copyright update to 2018 |
2016-01-09: | TWikibug:Item7708: Copyright update to 2016 |
2015-07-09: | TWikibug:Item7675: GET with support for JSON path using [*] wildcard array index |
2015-06-17: | TWikibug:Item7639: Document CPAN:JSON dependency |
2015-04-09: | TWikibug:Item7639: Use file locking for persistent store files to prevent corruption of store |
2015-04-09: | TWikibug:Item7635: SETGETDUMP can now dump volatile variables |
2015-04-09: | TWikibug:Item7636: Init variables at plugin init time (was not the case in a mod_perl environment) |
2015-04-07: | TWikibug:Item7635: Ability to specify store name to persistently store variables |
2015-03-31: | TWikibug:Item7611: Small doc fix |
2015-03-27: | TWikibug:Item7611: Support for JSON objects and JSON path |
2015-03-25: | TWikibug:Item7611: Switch debug flag from preferences setting to configure setting |
2015-01-15: | TWikibug:Item7604: Switch from GPL v2 to v3 |
2013-01-28: | TWikibug:Item7091: Use TWISTY in installation instructions and change history |
2012-11-12: | TWikibug:Item7032: GET variable with format parameter |
2012-11-12: | TWikibug:Item7020: Categorize TWiki Variable SET, GET, SETGETDUMP |
2012-10-11: | TWikibug:Item6978: SetGetPlugin::Core::_savePersistentVar() may fail at Data::Dumper->Dump(). "require Data::Dumper;" line added before that. -- TWiki:Main.HideyoImazu |
2012-09-25: | TWikibug:Item6943: Add REST interface |
2012-01-06: | TWikibug:Item6828: Removing write to persistent if already exists, and adding data dump -- TWiki:Main.AaronLWalker |
2011-07-09: | TWikibug:Item6725: Change global package variables from "use vars" to "our" |
2011-04-04: | TWikibug:Item6638: Small doc improvements |
2011-03-26: | TWikibug:Item6670: Make variables persistent across topic invocations |
2010-10-17: | TWikibug:Item6597: Initial version |
TWiki Dependency: | $TWiki::Plugins::VERSION 1.1 |
CPAN Dependencies: | CPAN:JSON - optional, required if JSON objects are used; CPAN:Clone - optional, required if JSON path with [*] wildcard array index is used |
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 |
Revision r7 - 2018-07-06 - 05:44:14 - TWikiContributor | Edit |