Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

User Macro, Check for empty $body

jonb August 13, 2020

https://confluence.atlassian.com/doc/user-macro-template-syntax-223906130.html

The $body object is available if you have specified that your macro has a body 

 

Is there a way to determine if the body is empty?  I've tried comparing to empty strings or true/false, but it seems that there's always some whitespace there (<p> or <br>).

 

I'm hoping to toggle some functionality based on if the user enters text in the body or not.

 

(I'm using the simply velocity context user macros)

1 answer

1 accepted

0 votes
Answer accepted
Bill Bailey
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 13, 2020

If you know exactly what is in the "empty" body, then you can compare it to that? Something like

if ($body=="<p> </p>")

Or more complex using RegEx

If ($body.matches("<p>\s+</p>"))

jonb August 13, 2020

I will try something like that.  It seems like confluence adds it's own <p> tags within the body by default, even if the user does not.

 

I guess those tags evaluate as "whitespace" in html.  Is there a way to check for any/all combinations of that?  remove any whitespace html tags?

Bill Bailey
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 14, 2020

Yes, \s is for any white space character. To do some experiments to just print out the $body to see what it actually contains. It may be that $body itself is empty, but the macro instance is being wrapped with p tags when the contents are empty -- HTML wants the content to be wrapped with something.

I did check one of my test macros and found this code, which works in the opposite sense

#set($display="")
## Check if the body exists
#if($body)
#set($display = $body.trim())
#end

If this doesn't help you, please post your macro code.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events