using cdata in velocity templates

has anyone used cdata in their velocity templates? My template always prints out the end ]]> when i preview a snippet templates that isn’t xml. Is there a way around this:

#set($text = $sys.item.getProperty(‘displaytitle’).String.replace("<p>", “”))##
#set($text2 = $text.replace("</p>", “”))##
#set($text3= $text2.replace("</a>", “”))##
#set($text4 = $text3.replace(">", ’ style=“font-weight:bold;color:rgb(247, 147, 30);text-decoration:underline;” target="_blank"><span style=“font-weight:bold;color:rgb(247, 147, 30);text-decoration:underline;”>’))##
#set($text5 = “</span></a>”)##
#define($data)$text4$text5#end
<![CDATA[$data]]>

Out put looks like this
<a><span></span></a>]]>

I don’t think this will work with an HTML or XHTML document.

HTML doesn’t know about the CDATA element, so the browser will handle this inconsistently. Firefox looks like it parses it (no trailing ]]>) but Chrome and IE will not. If you need to pass a CDATA block in text/html you could wrap it in a script tag so the browser doesn’t try to parse it.

Something like this should work:


<script type="text/javascript">
<![CDATA[$data]]>
</script>