You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Pavel Babachanakh <Pa...@uib.cherkassy.net> on 2001/12/06 13:57:42 UTC

Format, Alignt & Fill

Hi,

As far as I understand Velocity doesn't have any directives to
format, align or fill string variable.

I have got the following template:

123456789012345678901234567890123456789012345678901234567890
#set( $name = "Don" )
#set( $surname = "Walter" )
#set( $year = 1966 )
$name     $surname            $year

#set( $name = "Christopher" )
#set( $surname = "Williams" )
#set( $year = 1977 )
$name     $surname            $year


I got the following output:

123456789012345678901234567890123456789012345678901234567890
Don     Walter            1966

Christopher     Williams            1977

But I want a different output:

123456789012345678901234567890123456789012345678901234567890
Don       Walter              1966

ChristopheWilliams            1977


Therefore, I want the $name variable always to have 10 characters
length. If it is longer than 10 it must be reduced, if it
shorter then 10 it must be filled up to 10 characters. And I want
to indicate to what side the variable should be aligned - left
or right.  It's very important for output form where the
variable must be located in a definite place. IMHO, it
would be better to include into Velocity directives that
format string variables rather than code macro for the same
purpose.

Sincerely,
Pavel Babachanakh
paul@uib.cherkassy.net



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Format, Alignt & Fill

Posted by Simon Christian <si...@cpd.co.uk>.
Why not create a little tool which you put in the context that'll to the
job for you. A method which takes a String and integer length as
parameters, that either shortens the String to the specified length or
pads it out with spaces.

A very rough'n'ready example might be:

/*---------------------------------------------------------*/

public String trimOrPad(String myString, int myLength) {
   int len = myString.length();
   if(len < myLength) {
       for(int i = len; i < myLength; i++)
           myString = myString.concat(" ");
   } else if(len > myLength) {
     myString = myString.substring(0,len);
   }
   return myString;
}

/*---------------------------------------------------------*/

I'm sure you could do the same thing in a macro, and I'm equally sure
someone's created a more useful library in Java but there ya go..

- simon



Pavel Babachanakh wrote:
> 
> Hi,
> 
> As far as I understand Velocity doesn't have any directives to
> format, align or fill string variable.
> 
> I have got the following template:
> 
> 123456789012345678901234567890123456789012345678901234567890
> #set( $name = "Don" )
> #set( $surname = "Walter" )
> #set( $year = 1966 )
> $name     $surname            $year
> 
> #set( $name = "Christopher" )
> #set( $surname = "Williams" )
> #set( $year = 1977 )
> $name     $surname            $year
> 
> I got the following output:
> 
> 123456789012345678901234567890123456789012345678901234567890
> Don     Walter            1966
> 
> Christopher     Williams            1977
> 
> But I want a different output:
> 
> 123456789012345678901234567890123456789012345678901234567890
> Don       Walter              1966
> 
> ChristopheWilliams            1977
> 
> Therefore, I want the $name variable always to have 10 characters
> length. If it is longer than 10 it must be reduced, if it
> shorter then 10 it must be filled up to 10 characters. And I want
> to indicate to what side the variable should be aligned - left
> or right.  It's very important for output form where the
> variable must be located in a definite place. IMHO, it
> would be better to include into Velocity directives that
> format string variables rather than code macro for the same
> purpose.
> 
> Sincerely,
> Pavel Babachanakh
> paul@uib.cherkassy.net

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Format, Alignt & Fill

Posted by Christoph Reck <Ch...@dlr.de>.
Hi,

there should be nothing wrong in using a macro for this.
Some days ago somebody asked the same for padded numbers,
here the same for strings:

#macro( string10 $str )
#set( $len = $str.length )
#if( $len >= 10 )$str.substring(0,9)##EOLescaped
#else#set($pad = "          ")$pad.substring($len)$str##EOLescaped
#end
#end

Another trick to get nicely formatted comma separated lists is:
#macro( csv $list )
#set( $sep = "" )
#foreach( $item in $list )
$sep$item##EOLescaped
#set( $sep = ", " )
#end
#end

Hope this helps. 

Some functions like this should go into the org.apache.common.util.StringUtils
package (please ensure that they are well named, parametrized, no external
dependancies, and of general use). If the macro above is not acceptable, 
please submit a patch to that class with your enhancement (to the commons
list).

:) Christoph

Pavel Babachanakh wrote:
> 
> Hi,
> 
> As far as I understand Velocity doesn't have any directives to
> format, align or fill string variable.
> 
> I have got the following template:
> 
> 123456789012345678901234567890123456789012345678901234567890
> #set( $name = "Don" )
> #set( $surname = "Walter" )
> #set( $year = 1966 )
> $name     $surname            $year
> 
> #set( $name = "Christopher" )
> #set( $surname = "Williams" )
> #set( $year = 1977 )
> $name     $surname            $year
> 
> I got the following output:
> 
> 123456789012345678901234567890123456789012345678901234567890
> Don     Walter            1966
> 
> Christopher     Williams            1977
> 
> But I want a different output:
> 
> 123456789012345678901234567890123456789012345678901234567890
> Don       Walter              1966
> 
> ChristopheWilliams            1977
> 
> Therefore, I want the $name variable always to have 10 characters
> length. If it is longer than 10 it must be reduced, if it
> shorter then 10 it must be filled up to 10 characters. And I want
> to indicate to what side the variable should be aligned - left
> or right.  It's very important for output form where the
> variable must be located in a definite place. IMHO, it
> would be better to include into Velocity directives that
> format string variables rather than code macro for the same
> purpose.
> 
> Sincerely,
> Pavel Babachanakh
> paul@uib.cherkassy.net
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Format, Alignt & Fill

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 12/6/01 7:57 AM, "Pavel Babachanakh" <Pa...@uib.cherkassy.net> wrote:

> Hi,
> 
> As far as I understand Velocity doesn't have any directives to
> format, align or fill string variable.

That is correct.

> 
> I have got the following template:
> 
> 123456789012345678901234567890123456789012345678901234567890
> #set( $name = "Don" )
> #set( $surname = "Walter" )
> #set( $year = 1966 )
> $name     $surname            $year
> 
> #set( $name = "Christopher" )
> #set( $surname = "Williams" )
> #set( $year = 1977 )
> $name     $surname            $year
> 
> 
> I got the following output:
> 
> 123456789012345678901234567890123456789012345678901234567890
> Don     Walter            1966
> 
> Christopher     Williams            1977
> 
> But I want a different output:
> 
> 123456789012345678901234567890123456789012345678901234567890
> Don       Walter              1966
> 
> ChristopheWilliams            1977
> 
> 
> Therefore, I want the $name variable always to have 10 characters
> length. If it is longer than 10 it must be reduced, if it
> shorter then 10 it must be filled up to 10 characters. And I want
> to indicate to what side the variable should be aligned - left
> or right.  It's very important for output form where the
> variable must be located in a definite place. IMHO, it
> would be better to include into Velocity directives that
> format string variables rather than code macro for the same
> purpose.

Opinion noted.

I personally disagree, as you could come up with a nice set of formatting
tools that you just place in the context.  There already might be some in
jakarta-sandbox-commons/rupert, contributed by christoph and nathan.

If not, you could write and contribute a few :)

geir


-- 
Geir Magnusson Jr.                       geirm@optonline.net
System and Software Consulting
You're going to end up getting pissed at your software
anyway, so you might as well not pay for it. Try Open Source.



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>