Using HTML templates in macro development

Hi all,
I am new to macro development. I am developing a macro which sends REST calls to external API and want to show that data in the nicelly formatted table/HTML.
I followed this tutorial and all is good https://developer.atlassian.com/server/framework/atlassian-sdk/create-a-confluence-hello-world-macro/
My question is:
How do you include HTML templates (or possibly JSP snippets) in the macro.
Basically i see that we have JAvascript and CSS. I do not want to create my HTML inside java class:

public String execute(Map<String, String> map, String s, ConversionContext conversionContext) throws MacroExecutionException {
        return "<h1>Hello World</h1>";
    }

I think it is not a good practice. I want to have this HTML somewhere in the file and just include the tamplate so that it is processed and it linked to the Javascript and CSS file. I never seem to find an example of this.

Thank you for the time,
Branislav

You can return a velocity string then inside your velocity file you can add your HTML and your javascript.
eg.

Map<String, Object> context = MacroUtils.defaultVelocityContext();

public String execute(Map<String, String> map, String s, ConversionContext conversionContext) throws MacroExecutionException {
        return VelocityUtils.getRenderedTemplate("templates/ myfile.vm", context);
    }

Create the velocity template inside your resources folder
eg. myfile.vm

<div>
<script type="text/javascript">
    AJS.toInit(function () {
    ....
    });
</script>
</div>

You can also extract the JS part into another file if you want and use this #requireResourcesForContext("com.acme.plugin.fancy-context") inside your VM template to getting working

Doc

I hope this helps

1 Like

Thank you so much for your input. I will try this.

Kind regards,
Branislav