I am writing a user-macro and i am experiencing some strange beaviour with a boolean parameter. The docs tell me that a boolean parameter will pass "the value 'true' or 'false' to the macro as a string" (https://confluence.atlassian.com/x/lBmrEw).
If you provide no default then the parameter will only pass "true" to the macro. If the checkbox is unchecked in the macro dialog no value will be passed.
This behaviour changes if you provide a default:
A simple macro to test this behaviour:
## @param mybool:type=boolean #if ($parammybool == "true") true #elseif ($parammybool == "false") false #else nothing/novalue #end
Community moderators have prevented the ability to post new answers.
Matthew's suggestion is a good one, but I believe testing a variable with ! in Velocity is really checking whether it has been defined or not. This can be useful though because there is a bug in how Confluence handles default parameter values, described here: https://jira.atlassian.com/browse/CONF-23704
The following code sample takes 2 parameters, one of which is a boolean. We check both parameters to see if they exist, if not we re-apply the appropriate default values. The final if statement checks whether the debug flag is set (in the way Matthew explained).
## @param debug:title=Debug Mode|type=boolean|default=false ## @param maxValue:title=Maximum value|type=int|default=20 ## Check default values (bug in Confluence - https://jira.atlassian.com/browse/CONF-23704) #if(!$parammaxValue) #set($parammaxValue = 20) #end #if(!$paramdebug) #set($paramdebug = false) #end #if($paramdebug) DEBUG: Max Value = ${maxValue} #end
You could try adapting this to suit your parammybool and see what comes out.
So this is an already filed bug - i didnt found it. I went for a similar solution - check if the variable has a non-default value and then act accordingly.
Setting the default value manually is a good idea, although not necassary for booleans.
Good work, question answered.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
https://jira.atlassian.com/browse/CONF-23704 has been resolved since Confluence 5.5.1, so you may have to adapt your user macros, depending on how you workaround this issue. I edited the answer to properly reflect the status of the issue.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Try using syntax like this:
#if($parammybool)
This will check if parammybool is true without doing an explicit check.
To check if its false, do this:
#if(!$parammbool)
The ! indicates a "not".
Haven't tried this but it works in other languages.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
From the velocity docs (http://velocity.apache.org/engine/devel/docs/user-guide.html#Conditionals):
The variable $foo is evaluated to determine whether it is true, which will happen under one of two circumstances: (i) $foo is a boolean (true/false) which has a true value, or (ii) the value is not null
and from the user-macro docs (https://confluence.atlassian.com/x/lBmrEw):
Normally, a parameter like
$paramfoo
that is missing will appear as '$paramfoo' in the output. To display nothing when a parameter is not set, use an exclamation mark after the dollar sign like this:$!paramfoo
Good tip but it doesnt solve the problem of the defect handling/passing of the default values. If a variable has its default value, then no value/null is passed from confluence to the macro.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Your if...elseif block is comparing the value of $parammybool with string values, not booleans. If you remove the double quotes around "true" and "false" you should be fine.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Charles,
as written in the question the docs are telling me that confluence is passing "the value 'true' or 'false' to the macro as a string."
Anyway, i tried to comapre against boolean values instead of string - with the same effect. Values other than the (implicit) default value result in "no value".
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.