Replacing line breaks with HTML breaks in Velocity
Roller currently has an issue where line breaks in comments are not auto-converted to <br>'s. This problem only exists in the in-page comments and the twisty comments you see on this site. Today, I might've figured out the solution. It turns out that using Jakarta Common's StrutsUtils to replace new lines with <br>'s doesn't work:
#set($comments = $stringUtils.replace($comments, "\n", "<br />"))
However, using the String.replaceAll(new, old) in JDK 1.4 does work:
#set($comments = $comments.replaceAll("\n", "<br />"))
I figured this out on my current project and haven't tested it on Roller. Since I didn't find anything on this via Google - I though y'all might be interested.
Posted by Lance on March 11, 2004 at 02:23 AM MST #
Posted by Ramesh on March 11, 2004 at 03:56 AM MST #
Posted by Unknown on March 11, 2004 at 03:57 AM MST #
Posted by Unknown on March 11, 2004 at 03:58 AM MST #
Posted by Will Gayther on March 11, 2004 at 05:56 AM MST #
public void testCommonsStringUtils() throws Exception {
VelocityEngine engine = new VelocityEngine();
engine.init();
VelocityContext ctx = new VelocityContext();
ctx.put("stringUtils", new StringUtils());
ctx.put("comments", "this is a \n newline test");
ctx.put("newline", "\n");
ctx.put("break", "<br />");
String template = "#set($comments = $stringUtils.replace($comments,$newline,$break))";
template += "$comments";
StringWriter writer = new StringWriter();
engine.evaluate(ctx, writer, "", template);
assertEquals("this is a <br /> newline test", writer.toString());
}
Posted by Anonymous on March 12, 2004 at 05:09 PM MST #