| SetGetPlugin
|
|
< < | |
> > | |
|
|
|
Introduction |
|
< < | 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. |
> > | Use %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 are: |
> > | Alternatives to this plugin: |
| |
|
< < |
- TWiki preferences settings: Can be defined on a site level (TWikiPreferences), web level (WebPreferences) and topic level. Preferences settings persist until changed.
- SpreadSheetPlugin variables: Variables can be set with
%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).
|
> > |
- TWiki preferences settings: Can be defined on a site level (TWikiPreferences), web level (WebPreferences) and topic level. Preferences settings persist until changed.
- SpreadSheetPlugin variables: Variables can be set with
%CALCULATE{$SET(some_name, any value)}% and retrieved with $GET() . These variables persist during page rendering time.
|
|
Syntax Rules
SET{"name" value="..."} -- set a variable
- Set a named variable that can be retrieved later with
%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.
- Syntax:
%SET{ "name" value="..." remember="1" }%
- Parameters:
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. | "" |
- Example:
%SET{"lunch" value="Sushi"}%
- JSON Syntax:
%SET{ name = { ... } remember="1" }%
- An optional
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.
- Example to set, modify and get a JSON object:
-
%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"]}}
- Category: ApplicationsAndComponentsVariables, DevelopmentVariables
- Related: GET, IF, SETGETDUMP, SpreadSheetPlugin#FuncSET
|
|
< < | Important notes when using the remember option:
- Use descriptive variable names: Keep in mind that the remember option sets a variable with TWiki-global scope, e.g. for all pages and all users. To avoid name clashes, use a descriptive variable name which might include web & topic name and user or group name. For example, instead of variable named
"campaign" , use a more descriptive name "Marketing-Europe-2024-Campaign" .
|
> > | Important notes when using the remember or store option:
- Use descriptive variable names: Keep in mind that the remember option sets a variable with TWiki-global scope, e.g. for all pages and all users. To avoid name clashes, use a descriptive variable name which might include web & topic name and user or group name. For example, instead of variable name
"campaign" , use a more descriptive name "Marketing-Europe-2024-Campaign" .
|
|
- Do not store confidential content: The remembered variables are not aware of access control. If a user stores access controlled content in a variable, anyone who knows the variable name or uses the SETGETDUMP variable can read the content.
|
|
< < |
- Variables are not version controlled, e.g. you do not get the audit trail you have elsewhere in TWiki (topics, attachments, meta data, etc). Consider storing content in TWiki form fields if you need an audit trail.
|
> > |
- Variables are not version controlled, e.g. you do not get the audit trail available elsewhere in TWiki (topics, attachments, meta data, etc). Consider storing content in TWiki form fields if you need an audit trail.
|
|
GET{"name"} -- get a variable
- Get the value of a named variable, previously set with
%SET{}% .
- Syntax:
%GET{ "name" default="..." }%
- Parameters:
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. | "" |
- Example:
%GET{"lunch"}% returns Sushi if the following has been previously set: %SET{ "lunch" value="Sushi" }%
- JSON Syntax:
%GET{ name }%
- Example to set a JSON object and to get a JSON object with a JSON path:
-
%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"]
- Category: ApplicationsAndComponentsVariables, DevelopmentVariables
- Related: IF, SET, SETGETDUMP, SpreadSheetPlugin#FuncGET
SETGETDUMP{...} -- dump variables
- Dump volatile or persistent name/values previously set with
%SET{}% .
- Syntax:
%SETGETDUMP{ remember="1" format="..." separator="..." }%
- Parameters:
Parameter<-- --> | Description | Default | format="..." | Format output using variables $name and $value | "name: $name, value: $value <br />" | remember="1" | Dump all persistent variables | (volatile variables) | separator="..." | String used for separating entries | "\n" | store="..." | Dump variables of a specific store | (volatile variables) |
- Example:
%SETGETDUMP{"| $name | $value |" separator="$n"}%
- Category: ApplicationsAndComponentsVariables, DevelopmentVariables
- Related: GET, IF, SET, SpreadSheetPlugin#FuncSET
|
|
> > |
JSON Objects
JSON (JavaScript Object Notation) is a lightweight data-interchange format based on a subset of the JavaScript Programming Language. It is easy for humans to read and write, and easy for machines to parse and generate.
A JSON object can contain:
-
{ "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
A JSON object can be nested to any depth.
Using SET and GET it is possible to store and retrieve JSON objects, and parts thereof using a JSON path. A JSON path describes the path to an object within a JSON object.
JSON objects are useful in advanced TWiki applications that use JavaScript and Ajax calls. This plugin makes it easy to store and manipulate JSON objects.
Note: The CPAN:JSON module needs to be installed on the TWiki server.
SET a JSON Object
The syntax to set a JSON object is %SET{ name = object }% or %SET{ name.path = object }% .
-
name - variable name, such as: menu
- supported characters: alphanumeric, dash and underscore
-
.path - JSON path, such as: .File.Open[1] in menu.File.Open[1]
- JSON path fragment for a hash object is
.hashName , such as: .File
- supported characters for hash object name: alphanumeric, dash, underscore and dollar sign
- JSON path fragment for an array object is
[n] , such as: [1]
- supported characters for array index: non-negative integers
-
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
Setting a JSON object returns an empty string. In case of a parse error, the named variable contains the error message starting with 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 a JSON Object
The syntax to get a JSON object is %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
- supported characters: alphanumeric, dash and underscore
-
.path - JSON path, such as: .File.Open[1] in menu.File.Open[1]
- JSON path fragment for a hash object is
.hashName , such as: .File
- supported characters for hash object name: alphanumeric, dash, underscore and dollar sign
- JSON path fragment for an array object is
[n] , such as: [1]
- supported characters for array index: non-negative integers
[0]...[9999] and asterisk [*]
- use an
[*] asterisk wildcard to indicate all array items (only one wildcard can be used in a JSON path)
GET returns the JSON object or an object within it. The format depends on the type of object:
- hash object:
{"key1":"value1","key2":"value2"} - hash with key/value pairs; space trimmed; nondeterministic sequence; the value can be another JSON object
- array object:
["va1","val2","val3"] - array with values; space trimmed; the value can be another JSON object
- string object:
String - text string, without enclosing double quotes
JSON Examples
Example to set, modify and get a JSON object:
-
%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"]}}
Example to set and get a JSON array of hashes object, using JSON path with [*] 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"]
|
| |
|
< < | Examples |
> > | SET/GET Examples |
|
Set several times |
|
Last topic view |
|
< < | 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. |
> > | In a topic, set a variable to save the current using the remember flag. Add also a GET before the SET, it will show the time the topic has last been viewed. |
|
|
|
-
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)
|
|
Example:
https://iotworkshop.africa/do/rest/SetGetPlugin/set?name=rest-test;value=This+is+REST;remember=1 |
| 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)
|
|
> > |
-
store - specify a store name that holds a persistent variable (optional)
|
|
Example:
https://iotworkshop.africa/do/rest/SetGetPlugin/get?name=rest-test;default=rest-test+variable+not+found |
|
< < | Plugin Settings
<--/twistyPlugin twikiMakeVisibleInline-->
Plugin settings are stored as preferences variables. To reference a plugin setting write %<plugin>_<setting>% , i.e. %NEWPLUGIN_SHORTDESCRIPTION%
- One line description, is shown in the TextFormattingRules topic:
- Set SHORTDESCRIPTION = Set and get variables in topics, optionally persistently across topic views
- Debug plugin: (See output in
data/debug.txt )
<--/twistyPlugin--> |
| Plugin Installation Instructions
This plugin is pre-installed. TWiki administrators can upgrade the plugin as needed on the TWiki server. |
|
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 |
|
|
-
- Set the ownership of the extracted directories and files to the webserver user.
|
|
> > |
- Plugin dependencies:
- Install the CPAN:JSON module in case JSON objects are used.
- Install the CPAN:Clone module if JSON path with [*] wildcard array index is used.
|
|
- Plugin configuration and testing:
- Run the configure script and enable the plugin in the Plugins section.
- Test if the installation was successful: The next bullet should show:
Result: TWiki rocks!
|
|
Plugin Info |
|
> > |
- One line description, is shown in the TextFormattingRules topic:
- Set SHORTDESCRIPTION = Set and get variables and JSON objects in topics, optionally persistently across topic views
|
|
|
|
< < |
|
> > |
|
| <--/twistyPlugin twikiMakeVisibleInline--> |
|
> > |
|
|
|
| <--/twistyPlugin-->
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 |
|
|
|
|
|
|
< < | Related Topics: VarSET, VarGET, TWikiPlugins, DeveloperDocumentationCategory, AdminDocumentationCategory, TWikiPreferences, SpreadSheetPlugin |
> > | Related Topics: VarSET, VarGET, VarSETGETDUMP, TWikiPlugins, DeveloperDocumentationCategory, AdminDocumentationCategory, TWikiPreferences, SpreadSheetPlugin |