Is it possible to substitute parameters in an user macro's body

Petko October 21, 2021

Hello,

I would like to know if there is a possibility to define an user macro containing parameters and a $body,  where the macro should replace the parameters in it, when the macro is being "previewed" or displayed?

Example:

##example user macro template

## @param user:title=User|type=string|desc=set username|required=true|multiple=false|default=
$body

when using, the macro is included, parameter "user = m.mustermann" together with a body "Hello, $!user" is provided:

 

The goal is when the confluence page is saved or on preview to have this:

"Hello, max.mustermann"

I know that I can write following user macro:

##example user macro template

## @param user:title=User|type=string|desc=set username|required=true|multiple=false|default=

Hello, $!user

 and it works like a charm, but every time the body of the macro should be expanded with new information especially such using the parameter I should contact system administrator in order to get the new "version" of the macro which is kinda loosing the point of using such macro at the first place.

Thank you in advance

3 answers

1 vote
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.
February 4, 2022

Yes you can do what you want, but differently that you describe, What you want to do is retrieve the current user. 

#set($currentUser= $action.authenticatedUser)

The you would basically find the methods for name (getUserFullName I think will work). Parse that string for the first name, and bingo, it will alway display who is viewing the page.

Bonus

You can also access the user's group:

#set($groups = $userAccessor.getGroupNames($currentUser))

I have used this trick to hide buttons/functions from users not belong to the correct group.

 

If you want a nice display like Confluence, then:

<div class="content-wrapper"><p>
<ac:link>
<ri:user ri:userkey="$currentUser.key"/>
</ac:link>
</p>
</div>
Petko Ichev May 17, 2022

thank you for taking time to reply to my question. What you describe is handy and I will remember it so it may be of use in the future.

1 vote
Brant Schroeder
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 3, 2022

@Petko Welcome the Atlassian Community

Parameter values are provided by the user when they add or edit the macro.  Objects are available to you for certain page, space or content including parameters.  The object is what is eventually displayed in the macro or used to define page behavior.  You cannot have a parameter that is an object but you can have an object that is a parameter.  

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

Petko Ichev May 17, 2022

I've followed the provided guide in order to create my macro. Thank you for taking time to consider and reply to my question.

0 votes
Petko Ichev May 17, 2022

I believe my example was misinterpreted. The setup with the user was merely a try to provide an example of what I was trying to achieve namely to write a macro, where I am able to freely parameterize part of the information when I'm setting up the macro. I was able to create a macro with the text needed and parameters and every time I'm including the macro I just set values for the needed parameters and they get used in the text. The problem with this method is that each time when the text and perhaps new parameter is introduced I have to get in touch with the Administrator in order to have the body of the macro updated.

Actually later I found exactly what I was looking for: the plugin "Include Content" by Keysight Technologies- (https://marketplace.atlassian.com/apps/1217141/include-content?tab=overview&hosting=server).  Their " (include) shared block with replacement" macro is exactly what I was trying to achieve - to create a text block within a page and set parameters which should have their values set when I'm including the text block somewhere else in the documentation. And each time when I want to change the text or the parameters I just need to edit the source page.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events