It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Is there a way to place a list of the page's labels in a specific section?

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?

Thanks!

 

2 answers

1 accepted

1 vote
Answer accepted
Steven Behnke Community Leader Jul 23, 2016

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 – 

  • It likely isn't resistant to errors yet.
  • It offers no options or parameters.
  • The macro is simply placed and the Page can be saved.
  • It appears to hold up as expected through Include and Page Property Report macros.

If you have additional needs, I could probably address them here. Just let me know. smile

 

Final result

image2016-7-24 1:6:54.pngimage2016-7-24 1:6:18.png

image2016-7-24 1:11:22.png

Source

## 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>
Steven Behnke Community Leader Aug 03, 2016

If I can improve this macro or if you have any requests, let me know! User Macros are fun cheeky

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
Steven Behnke Community Leader Aug 05, 2016

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
Like 1 person likes this
Steven Behnke Community Leader Aug 05, 2016

We're using "velocity" scripting by the way. 

http://velocity.apache.org/engine/1.7/user-guide.html

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:

<span> </span>

 

I have no experience writing this kind of code, could you please help?

 

Many thanks,

Peter

Steven Behnke Community Leader Sep 23, 2018

@Peter Baschan please post your current macro source and the version of Confluence you're using.

@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

 

## @noparams
#set($currentPage = $pageManager.getPage($content.getId()))
#set($pageLabels = $currentPage.getLabels())
&lt;span&gt;
#foreach( $labelObject in $pageLabels )
&lt;span class="aui-label"&gt;$labelObject.name&lt;/span&gt;
#end
&lt;/span&gt;

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 '&lt;' and '&gt;' 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:

<environment_URL>/label/<space_name>/<label_text>

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.

Like 1 person likes this

@Peter Baschan

Here is what I did to make the labels clickable:

<span>
#foreach( $labelObject in $pageLabels )
<span class="aui-label">
<a href="$labelObject.urlPath">$labelObject.name</a>
</span>
#end
</span>

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted 5 hours ago in Jira

Upcoming Enterprise releases targeting Q3 2019: Jira Software 8.5 & Jira Service Desk 4.5

Since launching the Jira Software 8.0 and Jira Service Desk 4.0 platform releases in February, many of you have been asking when the next Enterprise release will be so you can take advantage of the m...

174 views 0 3
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you