velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nathan Bubna" <nbu...@gmail.com>
Subject Re: Formatting template in rows
Date Thu, 25 Oct 2007 03:08:25 GMT
n 10/24/07, Vasilis <vas_opel@yahoo.gr> wrote:
>
> Hi all ,
>
> I am using Velocity to create autogenerated mail messages. These messages
> will be in html and text format. I want to represent data in rows and
> columns like in a table. For the html format this is easy just using the
> velocity $for command and represent the data using <td> and <tr>. How can
I
> achieve this in the text without looing the table formatting?

unless all of your column values have the same number of characters,
this is not easily achieved.

> Below there is an example of what I want to do:
>
> Item           Price         VAT
> $item[1]     $price[1]     $vat[1]
> $item[2]     $price[2]     $vat[2]
> $item[..]     $price[..]    $vat[..]

you will need to somehow know the max number of characters per column
you have/want.  once you know that, you can create a tool class like
this:

public class CellFormatter {
    public String space(int size) {
        StringBuilder spaces = new StringBuilder();
        for(int i=0; i < size; i++)
             spaces.append(' ');
        return spaces.toString();
    }
    public String format(Object obj, int cellsize) {
        String value = String.valueOf(obj);
        if (value.length() == cellsize) {
             return value;
        } else if (value.length() > cellsize) {
             return value.substring(0, cellsize);
        } else {
             return value + space(cellsize - value.length());
        }
     }
}

put that into your context as "cell" (you can use VelocityTools to
automate this).

and use the it something like this:

#set( $cell = 10 )
$cell.format('Item', $cell)$cell.format('Price',
$cell)$cell.format('VAT', $cell)
#foreach( $row in [0..$rowCount] )
$cell.format($items.get($row), $cell)$cell.format($prices.get($row),
$cell)$cell.format($vats.get($row), $cell)
#end

of course, i haven't tested this code, but that's the basic idea.

> Thanks in advance,
> Roy
>
>
> --
> View this message in context: http://www.nabble.com/Formatting-template-in-rows-tf4683983.html#a13384548
> Sent from the Velocity - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Mime
View raw message