Feature Proposal: VAR{...} to get the value defined in a topic and the value in the current context
Motivation
As discussed in
AddTopicAttributeToVAR, there is no way to get the members of a TWiki group.
If there is a way to get the value of a preferences variable defined in a topic, we are golden.
%IF{"defined FOO" then="%FOO%"}% doesn't work well if FOO contains double quotes.
%IF{"defined FOO" then="$percntFoo$percnt"}% works well but it's cumbersome.
%VAR{"FOO"}% is close, but it does not retrieve the value of FOO if FOO is defined in the topic or Main.TWikiPreferences.
There should be a straightforward way to retrieve the value of a variable meeting the following requirements:
- If the variable is defined in the current context, its value is returned even if it contains double quote characters
- Otherwise, a null string ("") is returned
Description and Documentation
%VAR{...}% will be enhanced in the following manner.
- If the
topic parameter is specified, the value of the specified variable defined in the specified topic is returned.
- If the
web parameter is specified and the topic parameter is not specified, %VAR{...}% works in the same manner as before.
- If neither the
web nor topic parameter is specified, %VAR{...}% fetches the preferences variable or the session variable of the specified name in the current context.
Examples
Group members
%VAR{"GROUP" topic="Main.TWikiAdminGroup"}% returns the members of the TWikiAdminGroup
Hook in TWiki06x01 web topics
TWikiAccessControl can have
%VAR{TWIKI_ACCESS_CONTROL_TOP}% at the beginning so that the TWiki admin can put some sentences there by defining TWIKI_ACCESS_CONTROL_TOP on Main.TWikiPreferences.
Skin
Let's say you are writing the "foo" skin. And you want to have the users specify a string inserted in the head tag with FOOSKIN_HTML_HEAD_HOOK. That can be achieved by the following:
%VARVAL{"FOOSKIN_HTML_HEAD_HOOK" default="<nop>" ignorenull="on"}%
Here, additional parameters
default and
ignorenull are assumed. The effect of the
default parameter should be obvious. If
ignorenull is
"on", if the variable is defined but its value is null, %VAR{...}% behaves as if the variable is not defined.
Impact
Implementation
--
Contributors:
Hideyo Imazu - 2013-07-11
Discussion
Sensible enhancement to make
VarVAR handle the current topic scope in case the
web="" is not present.
On your example, this should work even with double quotes:
%IF{"defined FOO" then="$percntFOO$percnt"}%
--
Peter Thoeny - 2013-07-11
Ah, thank you for the tips. I didn't know $percnt and other escapes are processed in
then and
else parameter values. In TWiki 4.1, escapes in
then and
else parameters were not processed, which led me to believe that it's still the case.
--
Hideyo Imazu - 2013-07-12
See also related old
AddTopicAttributeToVAR proposal.
--
Peter Thoeny - 2013-07-12
AddTopicAttributeToVAR feature is now added.
--
Hideyo Imazu - 2013-07-12