Hi guys,
I have a number of custom user macros that rely on the contentbylabel macro to generate lists of pages. One thing that is annoying, however, is that if there are no pages that have a certain label attached to them then an error message is returned stating that no content can be found. I'd prefer it if nothing was shown at all.
I'd like to be able to wrap these contentbylabel macros in a condition that first checks whether any results exist and, if so, then run the contentbylabel macro. Alternatively, a smarter solution (so that two checks aren't performed) might be to replicate the behaviour of the contentbylabel macro and then build an array out of the results. If there are results then output them, else do nothing.
I'm a bit lost when I look at the Confluence API docs so does anyone know where I should be looking (or, better, could give me some sample code that achieves this)?
Thanks muchly
Community moderators have prevented the ability to post new answers.
Hi Steve,
I think the best solution would be to check whether the contentbylabel macro shows any results, and then simply not show it if there are none. Using this macro text, you should be able to figure it out.
## @noparams #set ( $contentByLabelResults = $action.helper.renderConfluenceMacro("{contentbylabel:labels=missing-label}") ) $generalUtil.escapeXml($contentByLabelResults) #if ( !$contentByLabelResults.contains("No content found for") ) $contentByLabelResults #end
As you can see, the renderConfluenceMacro method outputs the results as HTML and stores it in the velocity variable, and the content is displayed on the screen by escaping the HTML tags (next line, which can be removed; only shown for instructive purposes). If there are results (i.e. the "No content found for label(s):" message is not shown), then it will display the results.
If you have any other questions, let me know.
Excellent. That's simpler than what I had written. Many thanks Stephen.
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.