How to include a class in a user macro?
Asked by Manuel:

Hey there!

I am pretty a beginner in coding Confluence User Macros but have some experience in object oriented languages, such as VB and VBA. I never coded HTML, Java or CSS.

1.

Can I include other classes into my code, so I can use more functions, methods and properties. However I cannot find a tutorial to do this in a Confluence User Macro. For example I would like to show the mail address, personal information or phone number of a user.

I found [this page](https://confluence.atlassian.com/doc/user-macro-template-syntax-223906130.html "User Macro Template Syntax") that says I can use $config, $rederContext, $space and $content to access to these four classes, and I can use a bunch of other classes as described [here](https://developer.atlassian.com/server/confluence/confluence-objects-accessible-from-velocity/ "Confluence objects accessible from Velocity") . But nothing could yet help me out to display the user information.

(Maybe you can give me a link to a tutorial how to properly use [this page](https://docs.atlassian.com/atlassian-confluence/6.6.0/index.html "Atlassian Confluence 6.6.0 API"). That would be really nice. It's frustrating to find functions I can use in the editor.)

2.

Is it possible to define the data type of an object? Such as Key or User or ProfilePictureInfo etc.?

3.

Can you recommend an IDE to write User Macros or Plugins in Confluence or Jira? I just installed Visual Studio and Eclipse but the [Atlassian IDE Connector](https://confluence.atlassian.com/ideplugin/overview-of-the-atlassian-ide-connectors "Overview of the Atlassian IDE Connectors") does'nt seem to be compatible with the actual versions. (I really miss the auto complete function.. :-) )

Thank you in advance!

Manuel


Answers:
author: Bill Bailey
body: You have stumbled upon one of the vary powerful, but under-documented features of Confluence. To first give you a simple answer, yes you can included a class. For example, here is a simple macro that sets a class around the text inside the macro:

```
## Check if the body exists
#if($body)
#set($display = $body.trim())
#end

<p class="figtitle">$display</p>
```

Then take a look at my answer to a similar question [User-Macro-Language-Reference.](https://community.atlassian.com/t5/Confluence-questions/User-Macro-Language-Reference/qaq-p/1160755)

It should get you started down the rich and rewarding path of a user macro writer. ;-)

comments: author: Manuel
body: Hey [@Bill Bailey](/t5/user/viewprofilepage/user-id/502089) , thank you for your links. i really liked your article regarding the Wrapper Macro. That gave me some inspiration, since the motivation you describe is often like mine, too.

The links to the Velocity Engine showed me that I was already looking at the right places. Mabe I should read that site again more carefully. Thank you, though!

Your code example above ("\<p class ...") - as I understand it - defines a new class, right? That will be surely useful later. But my main question is:  
--\> Can I refer to a chosen class from the [Atlassian Confluence API](https://docs.atlassian.com/atlassian-confluence/6.6.0/index.html "Atlassian Confluence 6.6.0 API")?

Thank you!

Manuel


author: Bill Bailey
body: Sorry, you said class and I thought CSS. ;-) Yes you have access to Confluence JAVA objects and methods but there are limitations. Here is a list of objects available in the Confluence Velocity context:

[user-macro-template-syntax](https://confluence.atlassian.com/doc/user-macro-template-syntax-223906130.html#UserMacroTemplateSyntax-Objectsavailabletoyourmacro)

You can also add objects to the velocity context with some cleverness (this is not from me - I am not smart enough), for example:

```
## ContentPropertyManager setup
#set ($containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager'))
#set ($getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null))
#set ($containerManager=$getInstanceMethod.invoke(null,null))
#set ($containerContext=$containerManager.containerContext)
#set ($contentPropertyManager=$containerContext.getComponent('contentPropertyManager'))
```

So that I can do something like this to retrieve data stored by another macro:

```
## Retrieve doc title from scroll pagetitle macro and clean file name
#set($fileName=$contentPropertyManager.getTextProperty($content, "metadata.scroll.pagetitle"))
```

You can also add objects via a plugin, using the [velocity-context-module](https://developer.atlassian.com/server/confluence/velocity-context-module/). BTW, you can also package user macros using the [user-macro-module.](https://developer.atlassian.com/server/confluence/user-macro-module/)


author: Manuel
body: Hey [@Bill Bailey](/t5/user/viewprofilepage/user-id/502089) thank you for your detailed answer. This is exactly what I need to know.

Now just for a last outlook before I'll get "into the doing":

1. Are Plugins or Apps more open regarding those "limitations" compared to user macros?

2. What IDE do you recommend and how to tell it the language reference of confluence?

Thanks!

Manuel


author: Bill Bailey
body: Yes, plugins give you complete access to the API, but there is more overhead in development.

I tried to use an IDE before but wasn't that useful to me. I currently just use Notepad++ with Velocity language pack. I haven't tried it yet, but [@Davin Studer](/t5/user/viewprofilepage/user-id/781467) editor plug in looks promising.

I have used the SDK before for complex macro development as it spins up a dev version of Confluence -- its console gives valuable feedback.


author: Manuel
body: OK [@Bill Bailey](/t5/user/viewprofilepage/user-id/502089) thank you so far. I think I'll keep that in mind for when I have to break through the given limitations.


author: Davin Studer
body: Also, if you are going to be writing user macros I've written a free app in the marketplace for enhancing the user macro editing experience. There are a bunch of features lacking in the user macro exiting experience and I've tried to cover a bunch of the pain points.

[Enhanced User Macro Editor](https://marketplace.atlassian.com/apps/1218853/enhanced-user-macro-editor?hosting=server&tab=overview)

comments: author: Manuel
body: Hey [@Davin Studer](/t5/user/viewprofilepage/user-id/781467), thanks for your reply. And think what: I already had your plugin installed before I made this post. Great piece of work. Thanks for that!


author: Davin Studer
body: You are welcome. I'm glad it's helpful.


Tags: confluence-server

server