Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Velocity Wiki" for 
change notification.

The following page has been changed by MichaelGiroux:
http://wiki.apache.org/velocity/VelocityWhitespaceGobbleSetindentDirective

The comment on the change is:
Proposal for new #setindent directive

New page:
For the most part, I want my templates to be readable and I use indenting to 
accomplish that.  The indenting of the template has nothing to do with the 
desired output.  For example, a simple template to generate a Java class might 
look like this:
{{{ 
import com.myco.MyClass;
public Class AnotherClass {
#foreach ($t in $types)
    #if ($t.name.contains("_")
        Integer $t.name = 0;
        #if ($t.type == "static")
            // ignore static type $t.name
        #end
    #end
#end
}
}}} 
This template should produce something like:

{{{
import com.myco.MyClass;
public Class AnotherClass {
    Integer v_1 = 0;
    Integer v_2 = 0;
    // ignore static type s_0
}
}}}

Note that the indentation of the Integer statements and the // ignore .. 
comment are the same even though they have different amounts of leading 
whitespace in the template.
 
Given that each line of the nested blocks within template are indented further 
to improve readability, there is no good way for Velocity to know what I really 
want it to do.  There would need to be some marker in the template to indicate 
where indentation should be set for subsequent lines.

The same template can be modified slightly to indicate how to indent lines.
{{{ 
import com.myco.MyClass;
public Class AnotherClass {
    #setindent               ## a new setindent directive
#foreach ($t in $types)
    #if ($t.name.contains("_")
        Integer $t.name = 0;
        #if ($t.type == "static")
            // ignore static type $t.name
        #end
    #end
#end
}
}}} 

In this example, a #setindent directive specifies the starting location for all 
subsequent lines.  Velocity would use the whitespace before the #setindent 
directive as the amount to indent any subsequent text that is emitted.

This strategy assumes that all leading whitespace would be ignored, and 
indentation would be controlled exclusively by the #setIndent directives. 
 
I realize that whitespace processing is much more complicated that indentation 
processing alone. This proposal only addresses how velocity could be enhanced 
to control indentation of generated text.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to