Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

(What is wrong with) boolean parameter in macro

dirkd August 18, 2013

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:

  • default to true: only "false" or no value will be passed
  • default to false: only "true" or no value will be passed

A simple macro to test this behaviour:

## @param mybool:type=boolean
#if ($parammybool == "true")
true
#elseif ($parammybool == "false")
false
#else
nothing/novalue
#end

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
CharlesH
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 19, 2013

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.

dirkd August 19, 2013

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.

Tim
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 4, 2014

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.

0 votes
Matthew J. Horn
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 18, 2013

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.

dirkd August 19, 2013

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.

0 votes
CharlesH
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 18, 2013

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.

dirkd August 18, 2013

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".

TAGS
AUG Leaders

Atlassian Community Events