Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to render html tags on Velocity template?

Chong Jing Hong
Contributor
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_
Community Champion
June 14, 2021 edited

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
Contributor
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
atlassian, likes for trees, atlassian community, social impact, tree planting campaign, community kudos, atlassian giving, environmental impact, sustainability, likes for good, atlassian social responsibility, community challenge

Make every click count—help us plant 50,000 trees! 🌳

Want to make your everyday Community actions directly contribute to reforestation? The Atlassian Community can achieve this goal by liking a post, attending an ACE, sending your peers kudos, and so much more!

Help us plant more trees
AUG Leaders

Atlassian Community Events