How to render html tags on Velocity template?

Chong Jing Hong June 13, 2021

Hi, I'm developing a plugin that gets form values and displays them on a new page. The form values contain html tags. However, when I use ${variable-name}, the tags are not rendered. What do I do to render the html elements?

 

My action class:

package com.example.plugin;
import com.atlassian.confluence.core.ConfluenceActionSupport;
import com.opensymphony.xwork.Action;
public class ComparatorPluginResultAction extends ConfluenceActionSupport {        
private String pageVersion1;    
private String pageVersion2;
    public String execute() throws Exception {        return Action.SUCCESS;    }
    public String getPageVersion1() {        return this.pageVersion1;    }
    public String getPageVersion2(){        return this.pageVersion2;    }
    public void setPageVersion1(String pageVersion1){        this.pageVersion1 = pageVersion1;    }
    public void setPageVersion2(String pageVersion2){        this.pageVersion2 = pageVersion2;    }
}

 

My velocity template:

<!DOCTYPE HTML>
<html>    
<head>        
<title>Compare Results</title>        
<meta name="decorator" content="atl.general" />        
#requireResource(com.example.plugin.comparator-plugin:comparator-plugin-resources)
    
</head>    
<body>        
<div id="compare-div">            
<div id="col1">${pageVersion1}</div>            
<div id="col2">${pageVersion2}</div>        
</div>     
</body>
</html>
 

1 answer

1 accepted

0 votes
Answer accepted
Alex Medved _ConfiForms_
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.
June 14, 2021

This seems to be correct in general, but you need to put some values to "pageVersion1" and "pageVersion2" variables in your "execute" method

And for HTML tags you need to use @HtmlSafe annotation

com.atlassian.velocity.htmlsafe.HtmlSafe;

https://developer.atlassian.com/server/confluence/advanced-html-encoding/ 

Alex

Chong Jing Hong June 14, 2021

Hey Alex, thanks for the reply. I found a solution before seeing your answer. 

I added these 2 lines in the velocity template:

#set($pageVersion1WithHtml = $pageVersion1) <!-- to render html elements -->    
#set($pageVersion2WithHtml = $pageVersion2)

https://developer.atlassian.com/server/jira/platform/jira-6-0-html-escaping-for-velocity-templates/#references 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events