SetGetPlugin

Set and get variables in topics, optionally persistently across topic views

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.

%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:

  • 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).

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.
  • Syntax: %SET{ "name" value="..." remember="1" }%
  • Supported attributes:
    Attribute Comment 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"
  • Example: %SET{"lunch" value="Sushi"}%
  • Related: GET, IF, SETGETDUMP, SpreadSheetPlugin#FuncSET

ALERT! 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".
  • 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.

GET{"name"} -- get a variable

  • Get the value of a named variable, previously set with %SET{}%.
  • Syntax: %GET{ "name" default="..." }%
  • Supported attributes:
    Attribute Comment Default
    "name" Name of variable. (required)
    default="..." Text shown if variable is not found. "" (empty string)
  • Example: %GET{"lunch"}% returns Sushi if the following has been previously set:
    %SET{ "lunch" value="Sushi" default="undecided" }%
  • Related: IF, SET, SETGETDUMP, SpreadSheetPlugin#FuncGET

SETGETDUMP{...} -- dump persistent variables

  • Dump persistent key/values previously set with %SET{}%.
  • Syntax: %SETGETDUMP{ format="..." separator="..." }%
  • Supported attributes:
    Attribute Comment Default
    format="..." Format output using variables $key and $value "key: $key, value: $value <br />"
    separator="..." String used for separating entries "\n"
  • Example: %SETGETDUMP{"| $key | $value |" separator="$n"}%
  • Related: GET, IF, SET, SpreadSheetPlugin#FuncSET

Examples

Set several times

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:

  • Set "demo" to "uno":
  • Get "demo": uno
  • Set "demo" to "due":
  • Get "demo": due
  • Set "demo" to "tre":
  • Get "demo": tre

Search and save result

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:

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.

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:

  • Last view: 2024-12-21 11:07:46
  • Now: 2024-12-22 12:41:53

Remember my mood

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:
&nbsp; %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

Change my mood:

Plugin Settings

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)
    • Set DEBUG = 0

Plugin Installation Instructions

Note: You do not need to install anything on the browser to use this plugin. The following instructions are for the administrator who installs the plugin on the TWiki server.

  • For an automated installation, run the configure script and follow "Find More Extensions" in the in the Extensions section.

  • Or, follow these manual installation steps:
    • Download the ZIP file from the Plugins home (see below).
    • Unzip 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
    • Set the ownership of the extracted directories and files to the webserver user.

  • 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: Yes, it works!
      • Result: Yes, it works!

Plugin Info

Plugin Author: TWiki:Main.PeterThoeny
Copyright: © 2010-2012 TWiki:Main.PeterThoeny
© 2010-2012 TWiki:TWiki.TWikiContributor
License: GPL (GNU General Public Licen:se)
Plugin Version: 2012-01-06
Change History:  
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: 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

Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r2 - 2012-01-15 - TWikiContributor
 
This site is powered by the TWiki collaboration platform Powered by Perl This site is powered by the TWiki collaboration platformCopyright © 1999-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback
Note: Please contribute updates to this topic on TWiki.org at TWiki:TWiki.SetGetPlugin.