This question is in reference to Atlassian Documentation: Labels List Macro
One of the labels I'd like to use is a label indicating which product I'm creating documentation for. For example "ChevyNova". I know that the list of labels displays on the bottom of each confluence page by default.
I was wondering if there was a way to instead (or in addition) display the labels in a table in the main content of my page. That way it's visible for the end user and the editor doesn't have to add the data manually each time.
Basically I'd like something like the "Labels List" macro, that allowed you to limit it to the page you're on, not a specific space.
To illustrate, I'd like the label to appear here:screen.jpg
I've tried accomplishing this via CSS and html, but none of my efforts seem close so won't bother posting the code I've tried. I've found code that would allow me to move the labels to an absolute or relative position, but neither of these solutions would allow me to simply place it in a specific place inline with my html.
I tinkered around with editing the storage format code of the "List Labels" macro but I'm not even sure that could work in theory.
Anyone have any thoughts on how to approach the problem?
A user macro. If you are hosting your own server of Confluence and you are an Administrator, you can write a user macro to accomplish this. This example is extremely simple and offers no additional features. Notes and caveats –
If you have additional needs, I could probably address them here. Just let me know.
image2016-7-24 1:6:54.pngimage2016-7-24 1:6:18.png
## Macro title: Page labels ## Macro has a body: N ## Body processing: No macro body ## ## Developed by: Steve Behnke ## Date created: 24/07/2016 ## Installed by: Your name here... ## ## This macro lists all labels that are on this page. ## ## @noparams #set($currentPage = $pageManager.getPage($content.getId())) #set($pageLabels = $currentPage.getLabels()) <span> #foreach( $labelObject in $pageLabels ) <span class="aui-label">$labelObject.name</span> #end </span>
Thanks Steven! I'm learning as I work, so having that example is super helpful.
I'm trying to wrap my head around how I can display specific content if a page has a specific label, but I'm just not grasping how to handle the #if line. I've been able to figure out the various components, including how to display the content (I'm not a programmer by trade), but I can't seem to be able to figure this one line out.
I've tried working off of this macro: https://confluence.atlassian.com/display/DISC/if-label
It somewhat works for my needs, but I'm specifically trying to have Confluence display certain content based on labels. To clarify, the end result should be a user macro that dictates "IF THE PAGE HAS A LABEL 'DRAFT' PLACE THIS CONTENT, IF IT HAS A LABEL 'FINISHED' PLACE THIS CONTENT".
Any thoughts? Thanks again.
## @noparams #set($currentPage = $pageManager.getPage($content.getId())) #set($pageLabels = $currentPage.getLabels()) #foreach( $labelObject in $pageLabels) #if ( $label == true ) HI #end
That's a little more complicated. We need to add some logic to ensure that we only print the right labels. If we use the original for-each loop, we will print one result per label on the page! That wouldn't work at all.
So, we must check the labels and ensure that we store the ones that we want. Then we can print whatever we want. In this case, I am printing each matching label.
I haven't tested it much so let me know what you think.
## @noparams ## ## We can get the pageObject with the Page Manager ## $content is avaliable to us to query for the ID ## Then we get the labels of said page with the getLabels() method ## #set($currentPage = $pageManager.getPage($content.getId())) #set($pageLabels = $currentPage.getLabels()) ## ## Let's add a check to prevent working in case of no-labels at all. ## #if ($pageLabels.size() > 0) ## ##container for results ## #set($results=) ## ## We have a list of labels now, $pageLabels ## Let's loop through them and check for specific names with an if statement ## $labelObject will represent whatever label we're currently looping through ## #foreach( $labelObject in $pageLabels) #if ( $labelObject.name == "draft" ) #set($push = $results.add("draft")) #elseif ($labelObject.name == "finished") #set($push = $results.add("finished")) #end #end ## ## Finally we'll check list for each value and print that as labels ## #if($results.size() == 1) #foreach ($match in $results) #if ($match == "draft") <span class="aui-label">this is a draft</span> #elseif ($match == "finished") <span class="aui-label">this is finished</span> #end #end #elseif ($results.size() > 1) <span class="aui-lozenge aui-lozenge-error">multiple matching labels</span> #else <span class="aui-lozenge aui-lozenge-error">no matching labels</span> #end #else <span class="aui-lozenge aui-lozenge-error">no labels</span> #end
Hi @Steven Behnke,
I created a user macro using your simpler code above (just to list the labels without any logic), and on the page it shows this:
<span> <span class="aui-label">template</span> <span class="aui-label">testing</span> </span>
where the 'template' and 'testing' are the actual labels. If I delete the labels, it shows:
I have no experience writing this kind of code, could you please help?
@Steven Behnke sure, thanks for looking into this, I'm using Confluence 5.8.5 and the code is:
Macro Name: Page labels
Visibility: Visible to all users
Macro Title: Page labels
Description: Lists the labels added to the current page
Categories: Confluence Content
Icon URL: empty
Documentation URL: empty
Macro Body Processing: No macro body
#set($currentPage = $pageManager.getPage($content.getId()))
#set($pageLabels = $currentPage.getLabels())
#foreach( $labelObject in $pageLabels )
Hi @Steven Behnke. I have found the issue. When Atlassian migrated the community to a new platform, the code snippets provided in the discussions have been corrupted: the < and > characters become '<' and '>' respectively.
Changing these back to < and > will make the macro work.
On another note, is there a way to make these labels clickable just like the real tags at the bottom of the page? I recognize that the labels have a common syntax:
Sure, Macros are fun, but for such basic feature, it should be already implemented in Cloud version.
Confluence do a lot of thing, but not all the necessary things which can lead to: "Let's move on to another product". No multilanguage support, no "apply to". So beside "company internal needs", i don't see how this product can be used for our customers.
Hey Austin! Come join us for our 2019 Atlassian Community Summer BASH as we transition ourselves into party status! Bring an attachment, log a great time, and collaborate with new friends as we celeb...
Connect with like-minded Atlassian users at free events near you!Find an event
Connect with like-minded Atlassian users at free events near you!
Unfortunately there are no Community Events near you at the moment.Host an event
You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events