Motivation
There are cases where you want to determine if a preference variable is true or not for sure and safely.
Determining whether a preference is true or not is tricky. You can check if
%FOO_ENABLE% is "on" or not by
%IF{"$'FOO_ENABLE' = 'on'" ...}%. But ...
- A user may put extra space after "
* Set FOO_ENABLE = on", which sets "on " rather than "on" to FOO_NEABLE, in which case the condition "$'FOO_ENABLE' = 'on'" isn't met
- A user may set "1" rather than "on" meaning true
Description and Documentation
A new parameter
boolean will be introduced to the
VAR variable.
The parameter's value is evaluated by
TWiki::isTrue(). If it's true, the Boolean mode is turned on.
In the Boolean mode, the specified variable's value is handed to TWiki::isTrue(), whose result becomes the result of the
%VAR{...}%. TWiki::isTrue() returning either 0 or 1,
%VAR{...}% in the Boolean mode results in 0 or 1.
For example, if
FOO_ENABLE is set to "off",
%VAR{"FOO_ENABLE" boolean="on"}% will be expanded to 0.
%VAR{...boolean="on"...}% is rather verbose considering it will be used often. So a new variable
%ISTRUE{...}% will be introduced as a shorthand of
%VAR{...boolean="on"}%.
%ISTRUE{FOO_ENABLE}% is equivalent to
%VAR{"FOO_ENABLE" boolean="on"}%.
There might be cases where you want to specify true and false value. In that case, you will specify false and true values separated by comma.
Examples
%VAR{"FOO_ENABLE" boolean="false,true"}% will be expanded either
false or
true depending on the value of
%FOO_DISABLED%.
%ISTRUE{"FOO_ENABLE" boolean="false,true"}% will also work but the former looks better.
%ISTRUE{...}% will be handy in
%IF{...}%. e.g.
%IF{"%ISTRUE{FOO_ENABLE}%" then="..." else="..."}%.
Impact
Implementation
--
Contributors:
Hideyo Imazu - 2014-05-20
Discussion
Looks good!
--
Peter Thoeny - 2014-05-20