Jason Kary wrote: > Hi, > > I just had a quick question about your coding style? Can you explain why > you have #* *# in your code? My initial thoughts were to have an > obvious separation of VM code form HTML, however some of the code has #* > *# at the end of the line??? > > Thanks > Jason Kary > > >
Hi, Velocity has some quirks with whitespaces. Currently nicely structured templates produce ugly HTML souce, since whitespaces of indented directives are emitted to the output (except around #set(...) and #end statements). I have requested in the list to implement clear whitespace handling rules in the past within a thread called "Whitespace redux" started by Steven Hugg which got a good solution proposed by Daniel Dekany and me: > > If a line is looks like this: > DirectiveLine ::= LineEnd TabsAndSpaces? Directive TabsAndSpaces? LineEnd > TabsAndSpaces ::= (#x20 | #x09)+ > LineEnd ::= StartOfStream | ((#x0D #x0A) | #x0D | #x0A) | EndOfStream > then the first "TabsAndSpaces?" and the closing "TabsAndSpaces? LineEnd" > should be gobbled. (Note that it supports multiline directives, that is > why "DirectiveLine" is not simply "Line".) > If you want the first "LineEnd" to be gobbled prepend a ## before > it (makes it part of the comment!). > To have an indented directive remain indented, add a #**# before it. > > I guess this is the nearest we can get to a perfect solution. > But sorry to say the thread did not reach a consensus and a solution. Maybe its time to take another try on getting votes the proposed solution? So to get structured templates and output you currently need to "escape" the leading whitespaces of directives. This can be done either with a #* ... *# spanning the leading whitespaces or by adding a #* at each line-end and indenting the next line and then closing with *#. For Example: ## ------------------------------------------------------------------------ ## LOG ## ------------------------------------------------------------------------ ### allow toggling log display #if( !$hideLog && $log && ($log.size() != 0) ) #**#<br><font size="+1"><b>Log:</b></font><br> #**##foreach( $l_line in $log ) #* *##set( $l_line = $Context.encodeMarkup($l_line) ) #* *#<tt>$Regexp.substitute('s/ / /g', $l_line)</tt><br> #**##end #**#<br> #end Here the #if and #foreach emit a NL, the set gobbles it, the <tt> and <br> lines are passed as expected. So I get some extra lines in the output but no unexpected indentation. or use explicit $NL to insert line brakes where desired: #if( !$hideLog && $log && ($log.size() != 0) )#* *#<br><font size="+1"><b>Log:</b></font><br>$NL#* *##foreach( $l_line in $log )#* *##set( $l_line = $Context.encodeMarkup($l_line) )#* *#<tt>$Regexp.substitute('s/ / /g', $l_line)</tt><br>$NL#* *##end#* *#<br>$NL#* *##end -- :) Christoph Reck -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>