tag:help-archives.hannonhill.com,2010-02-09:/discussions/velocity-formats/681-serialization-and-linebreaksCascade CMS: Discussion 2013-08-20T01:47:42Ztag:help-archives.hannonhill.com,2010-02-09:Comment/246833292013-02-07T20:31:45Z2013-02-07T20:31:45ZSerialization and linebreaks<div><p>Hi,</p>
<p>It looks like <code>$_EscapeTool.javascript()</code> is a little
finicky with the serialized content, so what I did was create a
macro that escapes some of the special characters and line breaks
present within the content.</p>
<p>I was using an RSS feed in my example, but I hope the following
will get you started in the right direction.</p>
<pre>
<code>#macro (JSONEscape $str)$str.replace("${_EscapeTool.backslash}", "\\${_EscapeTool.backslash}").replace("${_EscapeTool.quote}", "\\${_EscapeTool.quote}").replace("'", "\'").replaceAll("\r\n", "")#end
<script type="text/javascript">
#set($items = $_XPathTool.selectNodes($contentRoot, '//item'))
var data = [
#foreach($item in $items)
#set($article_serialize = $_SerializerTool.serialize($item, true))
#set($article_serialize = "#JSONEscape($article_serialize)")
{
"article" : "${article_serialize}"
},
#end
];
}
</script></code>
</pre>
<p>Note: Velocity has an outstanding issue in which it will add
extra whitespace for every new line and tab, this is why I removed
all of the spacing from my macro. You may have to do the same
within your loop if you have issues with additional spacing.</p>
<p>Please let me know if you have any questions.</p>
<p>Thanks</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/246833292013-02-18T14:14:12Z2013-02-18T14:14:12ZSerialization and linebreaks<div><p>Hi Matthew,</p>
<p>Just wanted to follow up to see if you had a chance to view my
<a href=
"http://help.hannonhill.com/discussions/velocity-formats/681-serialization-and-linebreaks#comment_24693820">
recent comment</a>.</p>
<p>Please let us know if you have any questions.</p>
<p>Thanks.</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/246833292013-02-18T14:51:45Z2013-04-10T20:32:55ZSerialization and linebreaks<div><p>Yes Ryan. This is perfect. Thank you.</p></div>mlevytag:help-archives.hannonhill.com,2010-02-09:Comment/246833292013-02-18T15:12:21Z2013-02-18T15:12:21ZSerialization and linebreaks<div><p>Thank you for following up, Matthew. Glad to hear that did the
trick.</p>
<p>Have a great day!</p></div>Ryan Griffith