Arguments in favour of the Structured Whitespace Handling

The typical use scenario for velocity is to add VTL markup to text pages. Two forms can be differentiated:

  • a velocity directive on a line by itself
  • directives embedded within other text.

In the first case, it is desireable to allow indenting of the markup to better visualize the constructs; OTOH, the rendered output should not contain the whitespace indentation at the line beginning and the linefeed at the end. Example:

#foreach( $i in [1..3] )
  #if( $i > 1 )

For the second case, with embedded directives in a line, surrounding whitespaces should not be gobbled. A typical example of this from is:

You selected:#if( $value ) $value#else nothing#end/*
 * Copyright 2000-2004 The Apache Software Foundation.
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

This proposal does not deal/affect VTL-references.

Very rare cases exist where VTL-markup is embedded in non-line based files (binary?). For such uses, it is desireable to allow switching off the whitespace gobbling as a whole. A nice solution would be to add a pragma directive:

#pragma( whitespace-gobbling off ) 

This should turn the gobbling off only within the declaring template. It should not affect any outer template parsing a template declaring the whitespace handling pragma.

The implementation of this proposal implies that the velocity parser must be aware of whitespaces before and after directives, and that during rendering these are handled according to the whitepace handling pragma switch.

Early filtering implementation

Here is an implementation that uses a to filter unwanted whitespaces while loading the resource: StructuredGlobbingResourceLoader

  • No labels