You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Michael Giroux <ml...@gmail.com> on 2007/03/08 22:05:47 UTC
Re: Whitespace output control
I've been reviewing the various discussions on whitespace. I was not able
to determine if the current proposals cover my thoughts or not, so sorry if
what follows has been covered already.
For the most part, I want my templates to be readable and I use indenting to
accomplish that.
My template might appear as follows:
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 should produce something like:
import com.myco.MyClass;
public Class AnotherClass {
Integer v_1 = 0;
Integer v_2 = 0;
// ignore static type s_0
}
Given that each line of the nested blocks within template is indented
further, there would need to be some marker in the template to indicate
where indentation should be set for subsequent 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, (there are probably better ways) a #setindent directive
would specify the starting location for all subsequent lines. Velocity
would use the whitespace before the #setindent directive as the amount to
indent any text that is emitted. In this case, all leading whitespace would
be ignored, and indentation would be controlled by the #setIndent
directives.
As I said, if this has been covered already, I'm sorry for the duplicate.
Michael
Re: Whitespace output control
Posted by Robert Koberg <ro...@koberg.com>.
On Fri, 09 Mar 2007 11:47:04 -0500, <ap...@recks.org> wrote:
> Hi,
>
> Michael Giroux wrote:
>> On 3/9/07, apache@recks.org <ap...@recks.org> wrote:
>>
>>> Anyhow with the proposed VelocityWhitespaceGobbleStructuredTemplates
>>> you could achieve your desired output with either:
>>>
>>> import com.myco.MyClass;
>>> public Class AnotherClass {
>>> #foreach( $t in $types )
>>> #if( $t.name.contains("_") )
>>> #text( " Integer $t.name = 0;" )
>>> #if( $t.type == "static" )
>>> #text( " // ignore static type $t.name" )
>>> #end
>>> #end
>>> #end
>>> }
>>
>> But this requires that all output is encapsulated in #text() which
>> pretty
>> much eliminates the ability to copy/past text from other sources such as
>> working java programs.
>
> Yes and no - you can intermix the above with the below; thus
> leaving your source lines untouched!
BTW, this is basically how XSL does it.
-Rob
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Whitespace output control
Posted by ap...@recks.org.
Hi,
Michael Giroux wrote:
> On 3/9/07, apache@recks.org <ap...@recks.org> wrote:
>
>> Anyhow with the proposed VelocityWhitespaceGobbleStructuredTemplates
>> you could achieve your desired output with either:
>>
>> import com.myco.MyClass;
>> public Class AnotherClass {
>> #foreach( $t in $types )
>> #if( $t.name.contains("_") )
>> #text( " Integer $t.name = 0;" )
>> #if( $t.type == "static" )
>> #text( " // ignore static type $t.name" )
>> #end
>> #end
>> #end
>> }
>
> But this requires that all output is encapsulated in #text() which pretty
> much eliminates the ability to copy/past text from other sources such as
> working java programs.
Yes and no - you can intermix the above with the below; thus
leaving your source lines untouched!
>
>> or embedding the directives with a alternate indentation level to
>> optically make it stand out from the schmoo:
>>
>> 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 obscures the indenting of the template, making it difficult to
> maintain, at least for those of us who work better with nicely formatted
> files.
>
The "#"-syntax makes it easy for the eye to locate the directives. And
achived your "nicely formatted files" goal just the same
>
>> Your #setindent directive example would be hard for a parser to understand.
>> More standard would be #setindent(" ").
>
>
>
> Or perhaps #setindent(4) ## numeric value indicates amount of indent which
> allows something like
> #set ($indent = $indent +4)
> #setindent($indent)
>
The updated wiki page with #setindent([+-]<number>) syntax is much better!
>
>
>> But creating complex Java code
>> with many indented blocks would clutter the template with these directives
>> and not contribute to readability compared to the two above example approaches.
>> I would therefore vote against the proposal of the *SetindentDirective, which
>> could be also done using:
>> a) code formatter as Jalopy
>
>
> The same argument can be made for XML and HTML files, so why are we
> discussing whitespace at all? What about the case where the template is
> generating sample Java code to be included in a <code>...</code> block
> in an
> HTML document to be sent to a browser. There is no chance to run this
> through Jalopy.
;) I stated my previous post with "for one-time generated thingies ..."
I the case of good looking (managable) templates and dynamically generated
well structured output we need better whitespace handling and support.
>
> We currently use both Jalopy and/or the Eclipse JDT code formatter to
> reformat files that are generated for application code, but these have
> problems from time to time and Jalopy aborts without formatting. It is
> easier to view the generated file if it has some reasonable indentation.
>
>
>
>> b) the the VelocityWhitespaceGobbleAll and an custom #setindent()
>> directive
>> working together with a modified FileWriter
>
>
>
> When you say "custom #setindent()" does this say that it is not a standard
> part of Velocity? I think this should be standard.
It is quite easy to add custom directives per configuration...
>
>> Currently I use the ugly workaround in my macros:
>>
>> #macro( ClassMacro $indent $name $types )
>> ${indent}public Class $name {
>> #**#foreach( $t in $types )##
>> #* *#if( $t.name.contains("_") )##
>> ${indent} Integer $t.name = 0;
>> #* *#if ($t.type == "static")##
>> ${indent} // ignore static type $t.name
>> #* *#end##
>> #* *#end##
>> #**#end##
>> ${indent}}
>
>
> As you say, "ugly". Hopefully VelocityWhitespaceGobbleAll would eliminate
> the nead for the velocity comments #* *# at beginning of every line. It
> would be nice if these tricks were not needed, but the ${indent} would be
> tolerable if all the leading whitespace could be discarded and there was an
> easy way to populate $indent with desired number of blanks.
>
> import com.myco.MyClass;
> public Class AnotherClass {
> $set ($indent = $leadingBlanks.substring(0,$indentAmount))
> #foreach( $t in $types )
> #if( $t.name.contains("_") )
> ${indent} Integer $t.name = 0;
> #if ($t.type == "static")
> ${indent} // ignore static type $t.name
> #end
> #end
> #end
> }
>
> I think this might be equivalent to your suggestion to combine
> VelocityWhitespaceGobbleAll with a new #setindent(amount) directive.
> If Velocity is discarding all leading whitespace, and then injecting the
> amount specified by #setindent() a lot of the formatting issue would e resolved
> and in a lot of cases there would be no need for post processing the generated
> output.
Exaclty, but then you are near to my example with the #text(...) thingies ;)
That is why I suggested to combine this with a custom FileWriter that does this!
>
> Michael
>
You can see the reason for all of the proposed patters:
a) VelocityWhitespaceGobbleAsUsual
b) VelocityWhitespaceGobbleAll
c) VelocityWhitespaceGobbleNone
d) VelocityWhitespaceGobbleStructuredTemplates
Yours can be painlessly achieved with b) plus #setindent(...) and a FileWriter
fitting perfectly your application. OTOH d) would also do the job ;)
Cheers,
Christoph
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Whitespace output control
Posted by Michael Giroux <ml...@gmail.com>.
On 3/9/07, apache@recks.org <ap...@recks.org> wrote:
> Anyhow with the proposed VelocityWhitespaceGobbleStructuredTemplates
> you could achieve your desired output with either:
>
> import com.myco.MyClass;
> public Class AnotherClass {
> #foreach( $t in $types )
> #if( $t.name.contains("_") )
> #text( " Integer $t.name = 0;" )
> #if( $t.type == "static" )
> #text( " // ignore static type $t.name" )
> #end
> #end
> #end
> }
But this requires that all output is encapsulated in #text() which pretty
much eliminates the ability to copy/past text from other sources such as
working java programs.
> or embedding the directives with a alternate indentation level to
> optically make it stand out from the schmoo:
>
> 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 obscures the indenting of the template, making it difficult to
maintain, at least for those of us who work better with nicely formatted
files.
> Your #setindent directive example would be hard for a parser to
> understand.
> More standard would be #setindent(" ").
Or perhaps #setindent(4) ## numeric value indicates amount of indent which
allows something like
#set ($indent = $indent +4)
#setindent($indent)
> But creating complex Java code
> with many indented blocks would clutter the template with these directives
> and not contribute to readability compared to the two above example
> approaches.
> I would therefore vote against the proposal of the *SetindentDirective,
> which
> could be also done using:
> a) code formatter as Jalopy
The same argument can be made for XML and HTML files, so why are we
discussing whitespace at all? What about the case where the template is
generating sample Java code to be included in a <code>...</code> block in an
HTML document to be sent to a browser. There is no chance to run this
through Jalopy.
We currently use both Jalopy and/or the Eclipse JDT code formatter to
reformat files that are generated for application code, but these have
problems from time to time and Jalopy aborts without formatting. It is
easier to view the generated file if it has some reasonable indentation.
> b) the the VelocityWhitespaceGobbleAll and an custom #setindent()
> directive
> working together with a modified FileWriter
When you say "custom #setindent()" does this say that it is not a standard
part of Velocity? I think this should be standard.
> Currently I use the ugly workaround in my macros:
>
> #macro( ClassMacro $indent $name $types )
> ${indent}public Class $name {
> #**#foreach( $t in $types )##
> #* *#if( $t.name.contains("_") )##
> ${indent} Integer $t.name = 0;
> #* *#if ($t.type == "static")##
> ${indent} // ignore static type $t.name
> #* *#end##
> #* *#end##
> #**#end##
> ${indent}}
As you say, "ugly". Hopefully VelocityWhitespaceGobbleAll would eliminate
the nead for the velocity comments #* *# at beginning of every line. It
would be nice if these tricks were not needed, but the ${indent} would be
tolerable if all the leading whitespace could be discarded and there was an
easy way to populate $indent with desired number of blanks.
import com.myco.MyClass;
public Class AnotherClass {
$set ($indent = $leadingBlanks.substring(0,$indentAmount))
#foreach( $t in $types )
#if( $t.name.contains("_") )
${indent} Integer $t.name = 0;
#if ($t.type == "static")
${indent} // ignore static type $t.name
#end
#end
#end
}
I think this might be equivalent to your suggestion to combine
VelocityWhitespaceGobbleAll with a new #setindent(amount) directive. If
Velocity is discarding all leading whitespace, and then injecting the amount
specified by #setindent() a lot of the formatting issue would e resolved and
in a lot of cases there would be no need for post processing the generated
output.
Michael
Re: Whitespace output control
Posted by ap...@recks.org.
for one-time generated thingies like HTML or Java classes, you
can easily post-process the Velocity output with a code formatter.
Anyhow with the proposed VelocityWhitespaceGobbleStructuredTemplates
you could achieve your desired output with either:
import com.myco.MyClass;
public Class AnotherClass {
#foreach( $t in $types )
#if( $t.name.contains("_") )
#text( " Integer $t.name = 0;" )
#if( $t.type == "static" )
#text( " // ignore static type $t.name" )
#end
#end
#end
}
or embedding the directives with a alternate indentation level to
optically make it stand out from the schmoo:
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
}
Your #setindent directive example would be hard for a parser to understand.
More standard would be #setindent(" "). But creating complex Java code
with many indented blocks would clutter the template with these directives
and not contribute to readability compared to the two above example approaches.
I would therefore vote against the proposal of the *SetindentDirective, which
could be also done using:
a) code formatter as Jalopy
b) the the VelocityWhitespaceGobbleAll and an custom #setindent() directive
working together with a modified FileWriter
Currently I use the ugly workaround in my macros:
#macro( ClassMacro $indent $name $types )
${indent}public Class $name {
#**#foreach( $t in $types )##
#* *#if( $t.name.contains("_") )##
${indent} Integer $t.name = 0;
#* *#if ($t.type == "static")##
${indent} // ignore static type $t.name
#* *#end##
#* *#end##
#**#end##
${indent}}
Just my 2c...
:) Christoph
Michael Giroux wrote:
> I've been reviewing the various discussions on whitespace. I was not able
> to determine if the current proposals cover my thoughts or not, so sorry if
> what follows has been covered already.
>
> For the most part, I want my templates to be readable and I use
> indenting to
> accomplish that.
>
> My template might appear as follows:
>
> 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 should produce something like:
> import com.myco.MyClass;
> public Class AnotherClass {
> Integer v_1 = 0;
> Integer v_2 = 0;
> // ignore static type s_0
> }
>
> Given that each line of the nested blocks within template is indented
> further, there would need to be some marker in the template to indicate
> where indentation should be set for subsequent 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, (there are probably better ways) a #setindent directive
> would specify the starting location for all subsequent lines. Velocity
> would use the whitespace before the #setindent directive as the amount to
> indent any text that is emitted. In this case, all leading whitespace
> would
> be ignored, and indentation would be controlled by the #setIndent
> directives.
>
> As I said, if this has been covered already, I'm sorry for the duplicate.
>
> Michael
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Whitespace output control
Posted by Michael Giroux <ml...@gmail.com>.
On 3/8/07, Nathan Bubna <nb...@gmail.com> wrote:
>
> If you would like this to be remembered, i'd recommend adding it to
> the Wiki page for the whitespace issue:
>
> http://wiki.apache.org/velocity/VelocityWhitespaceGobbling
I have added a new proposal to the wiki.
Re: Whitespace output control
Posted by Nathan Bubna <nb...@gmail.com>.
This is either the first time i've heard a proposal like this, or else
those who've made such suggestions have not been persistent in them.
If you would like this to be remembered, i'd recommend adding it to
the Wiki page for the whitespace issue:
http://wiki.apache.org/velocity/VelocityWhitespaceGobbling
And as with all the proposals, i've long suspected that patches are
likely to be the deciding factor. I don't think anyone has so far
produced patches to support their choice (though Christoph may once
upon a time have produced a parser grammar or something like that).
And, as things stand, the consensus seems to be that none of the
proposals are likely to be implemented before Velocity 2.0, due to
concerns over backwards compatibility.
That's not to say, of course, that it is impossible to implement one
of these in a way that is backwards compatible. It just means that no
one has done it.
On 3/8/07, Michael Giroux <ml...@gmail.com> wrote:
> I've been reviewing the various discussions on whitespace. I was not able
> to determine if the current proposals cover my thoughts or not, so sorry if
> what follows has been covered already.
>
> For the most part, I want my templates to be readable and I use indenting to
> accomplish that.
>
> My template might appear as follows:
>
> 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 should produce something like:
> import com.myco.MyClass;
> public Class AnotherClass {
> Integer v_1 = 0;
> Integer v_2 = 0;
> // ignore static type s_0
> }
>
> Given that each line of the nested blocks within template is indented
> further, there would need to be some marker in the template to indicate
> where indentation should be set for subsequent 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, (there are probably better ways) a #setindent directive
> would specify the starting location for all subsequent lines. Velocity
> would use the whitespace before the #setindent directive as the amount to
> indent any text that is emitted. In this case, all leading whitespace would
> be ignored, and indentation would be controlled by the #setIndent
> directives.
>
> As I said, if this has been covered already, I'm sorry for the duplicate.
>
> Michael
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org