Velocity whitespace handling is not as written

Oct 29, 2003 22:17 · 213 words · 1 minute read

In the Velocity User Guide, it says that “Velocity’s behaviour is to gobble up excess whitespace.” and gives some examples that it says will produce the same output. That’s not the way it works for me:

public void testVelocityWhitespace()
{
  VelocityEngine ve = new VelocityEngine();
  try
  {
    ve.init();
    VelocityContext context = new VelocityContext();
    String test1 = "Send me #set($foo = ["$10 and ","a cake"])#foreach($a in $foo)$a #end please.";
    String test2 = "Send me\n" +
      "#set( $foo = ["$10 and ","a cake"] )\n" +
      "#foreach( $a in $foo )\n" +
      "$a\n" +
      "#end\n" +
      "please.";
    String test3 = "Send me\n" +
      "         #set($foo       = ["$10 and ","a cake"])\n" 
      "                          #foreach           ($a in $foo )$a\n" +
      "                  #end please.";
    StringWriter sw = new StringWriter();
    ve.evaluate(context, sw, "test", test1);
    String output1 = sw.toString();
    sw = new StringWriter();
    ve.evaluate(context, sw, "test", test2);
    String output2 = sw.toString();
    sw = new StringWriter();
    ve.evaluate(context, sw, "test", test3);
    String output3 = sw.toString();
    System.out.println(output1);
    System.out.println(output2);
    System.out.println(output3);
    assertEquals(output1, output2);
    assertEquals(output1, output3);
  }
  catch (Exception e)
  {
    e.printStackTrace();
    fail("Template didn't work correctly.");
  }
}

Perhaps there’s a property that controls it. Either way, the documentation should probably change to reflect what Velocity really does. (By the way, this is not at all a knock on Velocity. I think it’s a very nice templating system.)