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

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

Ameen Belbahri July 22, 2016

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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Steven F Behnke
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.
July 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 F Behnke
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 3, 2016

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

Ameen Belbahri August 4, 2016

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 F Behnke
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 5, 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 Nathanael Motz likes this
Steven F Behnke
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 5, 2016

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

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

Peter_Baschan September 21, 2018

Hi @Steven F 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 F Behnke
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.
September 23, 2018

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

Peter_Baschan September 24, 2018

@Steven F 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;

Peter_Baschan September 26, 2018

Hi @Steven F 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>

Christian de Bellefeuille October 25, 2018

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 publicmarvin likes this
1 vote
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 1, 2019

@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>
Dirk Lindner May 2, 2019

This is exactly what I was looking for! Thank you!

TAGS
AUG Leaders

Atlassian Community Events