Formatting of source code (Pretty Print of Source)

Does anyone out there know how or where to adjust how the output for the velocity templates source is?
Is it even possible to make the velocity html out pretty print?

Thanks for any help?

You can highlight any section of a Velocity template in the Workbench, right-click, and select “format…”. This will “reformat” your Velocity according to the rules/preferences selected for VeloEclipse.

Thanks that is good to know but I was more interested in how I could format the HTML source from the velocity templates, if that was even possible?

I’m not sure exactly what you’re asking here.

Velocity preserves whitespace in the page EXCEPT for whitespace in directives.

See the user guide: http://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html#vtl:_formatting_issues

This means that any whitespace in the template will appear in the output. Adding whitespace (to “pretty print” the Velocity template source) should appear in the output.

Well I can best explain it with an example. Okay, you have a list and you are searching for a specific value on one or more of the nodes using a foreach loop. You find a node with the value you are looking for at the 10th node. Now when you view the HTML source output for that velocity template you have 9 blank lines. Then you get (n - 10) extra blank lines after that node is printed, ‘n’ being the number of nodes in the list greater than ten. Is there a way to clean this up either in the velocity template itself or in a file that controls the out put file from velocity?

Thanks, me and my coworkers will take a look at that document.

In my previous post, I said that Velocity outputs all whitespace, except the whitespace inside a directive.

If you write a loop like this:

#foreach($foo in $bar)
     #if($foo.x == 'y')
            xyz
     #end
#end

You will get 2 extra lines for each line in the loop (the newline before the #if, and the newline after the first #end) as well as some extra spaces in the output.

If you write the same loop like this:

#foreach($foo in $bar)#if($foo.x == 'y')xyz#end#end

There will be no extra whitespace.

This can be rather painful, and it greatly reduces the “readability” and “maintainability” of the code, but in the absence of changes in the Velocity language itself, there’s not a lot we can do about this.

There has been some debate about this in the Velocity community. See this page (and its related links)

http://wiki.apache.org/velocity/VelocityWhitespaceGobbling

It looks like this is on the roadmap for Velocity 2.0, but this release is still “under discussion” (there will be a 1.6 before there is a 2.0).

Thank you, that answers my supers question perfectly.