You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Travis Low <tr...@dawnstar.org> on 2000/09/08 16:28:11 UTC

arithmetic

Is there room in Velocity for integer arithmetic?  Designers need it. 
For example, there may be a dynamic number of columns in an HTML
table, and the designer may wish to specify the width attribute for
each column as a percentage of the available area.  Or think about
displaying a calendar month -- padding might be needed for the first
and last weeks of the month.  Or they may wish to number items in a
list.  Or display table rows that cycle through three or more colors. 
All of this is currently possible without arithmetic, but it's ugly.

-- Travis Low  
   <ma...@dawnstar.org>
   <http://dawnstar.org/travis>

Re: arithmetic

Posted by Jason van Zyl <jv...@periapt.com>.
Hi,

A simple text file will do. If you send it to the
list I will place it in the specification directory.
We can put proposals in there as well for now.

I'm playing with arithmetic right now, just to
see how easy it is to get working. Right now
it's something like

$foo = $foo + 1

maybe

#math $foo = $foo + 1

Just send whatever you do to the list and I'll
check it in.

jvz.

On Fri, 8 Sep 2000, Travis Low wrote:

> Regarding a spec:
> 
> Is there a particular graphic format I should use?  If it's
> the new XSL/XML/whateverML stuff, I'll need some guidance.  Otherwise,
> should I use an HTML page or a long text message to the list?  (for
> the last, I could break it into multiple messages by directive or
> keyword or something)
> 
> Are there existing specs I should follow (for style or otherwise)?
> 
> Does the entire language need to be documented/specified, or just the
> arithmetic?
> 
> Hmmm, MVC Template Language only saves me a syllable.  V2, V-prime,
> Speed, anything would be better.  :-)
> 
> -- Travis Low  
>    <ma...@dawnstar.org>
>    <http://dawnstar.org/travis>
> 
> Jon Stevens wrote:
> > 
> > on 9/8/2000 1:09 PM, "Travis Low" <tr...@dawnstar.org> wrote:
> > 
> > > I've been thinking about this for six months now, so I think I've
> > > identified most of the issues.  So I'll write up a detailed proposal.
> > > Thanks for the invitation.
> > 
> > great! That is exactly what we need. That can be the start of our entire
> > proposal.
> > 
> > > Say, perhaps you would consider naming the language separately from
> > > the template engine?  So we don't have to say "Velocity template
> > > engine" and "Velocity template language".
> > 
> > How about:
> > 
> > MVC Template Language
> > 
> > :-)
> > 
> > -jon
> 

-- 

Jason van Zyl
jvanzyl@periapt.com


Re: arithmetic

Posted by Travis Low <tr...@dawnstar.org>.
Regarding a spec:

Is there a particular graphic format I should use?  If it's
the new XSL/XML/whateverML stuff, I'll need some guidance.  Otherwise,
should I use an HTML page or a long text message to the list?  (for
the last, I could break it into multiple messages by directive or
keyword or something)

Are there existing specs I should follow (for style or otherwise)?

Does the entire language need to be documented/specified, or just the
arithmetic?

Hmmm, MVC Template Language only saves me a syllable.  V2, V-prime,
Speed, anything would be better.  :-)

-- Travis Low  
   <ma...@dawnstar.org>
   <http://dawnstar.org/travis>

Jon Stevens wrote:
> 
> on 9/8/2000 1:09 PM, "Travis Low" <tr...@dawnstar.org> wrote:
> 
> > I've been thinking about this for six months now, so I think I've
> > identified most of the issues.  So I'll write up a detailed proposal.
> > Thanks for the invitation.
> 
> great! That is exactly what we need. That can be the start of our entire
> proposal.
> 
> > Say, perhaps you would consider naming the language separately from
> > the template engine?  So we don't have to say "Velocity template
> > engine" and "Velocity template language".
> 
> How about:
> 
> MVC Template Language
> 
> :-)
> 
> -jon

Re: arithmetic

Posted by Jon Stevens <jo...@latchkey.com>.
on 9/8/2000 1:09 PM, "Travis Low" <tr...@dawnstar.org> wrote:

> I've been thinking about this for six months now, so I think I've
> identified most of the issues.  So I'll write up a detailed proposal.
> Thanks for the invitation.

great! That is exactly what we need. That can be the start of our entire
proposal.

> Say, perhaps you would consider naming the language separately from
> the template engine?  So we don't have to say "Velocity template
> engine" and "Velocity template language".

How about:

MVC Template Language

:-)

-jon

-- 
http://scarab.tigris.org/    | http://noodle.tigris.org/
http://java.apache.org/      | http://java.apache.org/turbine/
http://www.working-dogs.com/ | http://jakarta.apache.org/velocity/
http://www.collab.net/       | http://www.sourcexchange.com/



Re: arithmetic

Posted by Travis Low <tr...@dawnstar.org>.
Jon Stevens wrote:

> When you propose things, you need to start proposing them in a specification
> point of view and consider all the different possibilities of how it would
> be implemented in the templates.

I've been thinking about this for six months now, so I think I've
identified most of the issues.  So I'll write up a detailed proposal. 
Thanks for the invitation.

Say, perhaps you would consider naming the language separately from
the template engine?  So we don't have to say "Velocity template
engine" and "Velocity template language".

BTW, I think you should always -1 bad suggestions, so if you did -1
all my suggestions, I'd just think they were mostly bad suggestions. 
In other words, I wouldn't take it personally.  Life is too short.

-- Travis Low  
   <ma...@dawnstar.org>
   <http://dawnstar.org/travis>

Re: arithmetic

Posted by Jason van Zyl <jv...@periapt.com>.
On Fri, 8 Sep 2000, Eric J Altman wrote:

> Hi,
> 
> Jon Stevens wrote:
> > 
> > The problem is if you want to do something like this:
> > 
> > <table>
> >     <tr>
> >         <td>1.</td>
> >         <td>Foo</td>
> >     </tr>
> >     <tr>
> >         <td>2.</td>
> >         <td>Bar</td>
> >     </tr>
> > </table>
> > 
> 
> Would this be easier, or am I missing your point here?  Maybe external
> to velocity in Turbine add a method to the formatter which works like
> the alternator and then (messy pseudo wm code):
> 
> <table>
> $formatter.makeCounter(counter, "1")
> #foreach $line in $allLines
> <tr>
>    <td>$counter</td>
>    <td>$line</td>
> </tr>
> $counter.increment()
> #end
> </table>
> 
> But it sounds like jvz is going to add something ("_item") right into
> the velocity spec which would be preferable.

Haven't done it yet but it would be dead simple if that
sort of info is handy during an iteration. Just a simple
insertion into the context before an iteration and
remove it when it's done.

jvz.

-- 

Jason van Zyl
jvanzyl@periapt.com


Re: arithmetic

Posted by Eric J Altman <ea...@speakeasy.org>.
Hi,

Jon Stevens wrote:
> 
> The problem is if you want to do something like this:
> 
> <table>
>     <tr>
>         <td>1.</td>
>         <td>Foo</td>
>     </tr>
>     <tr>
>         <td>2.</td>
>         <td>Bar</td>
>     </tr>
> </table>
> 

Would this be easier, or am I missing your point here?  Maybe external
to velocity in Turbine add a method to the formatter which works like
the alternator and then (messy pseudo wm code):

<table>
$formatter.makeCounter(counter, "1")
#foreach $line in $allLines
<tr>
   <td>$counter</td>
   <td>$line</td>
</tr>
$counter.increment()
#end
</table>

But it sounds like jvz is going to add something ("_item") right into
the velocity spec which would be preferable.

Regards,
~Eric J Altman

Re: arithmetic

Posted by Jason van Zyl <jv...@periapt.com>.
Hi,

I have started adding proper conditional expressions
and arithmetic, they come in a package. I adapted the
grammar from another language. Right now it would
working something like

$foo = $foo + 1

But it might have to be adapted to #math $foo = $foo + 1.
I'm not sure yet.

On Fri, 8 Sep 2000, Jon Stevens wrote:

> on 9/8/2000 1:41 PM, "Eric J Altman" <ea...@speakeasy.org> wrote:
> 
> > Something like Sean's Turbine WM utility class to alternate row color is
> > much nicer than adding this stuff in anyway, but whatever.
> > 
> > Regards,
> > ~Eric J Altman
> 
> The problem is if you want to do something like this:
> 
> <table>
>     <tr>
>         <td>1.</td>
>         <td>Foo</td>
>     </tr>
>     <tr>
>         <td>2.</td>
>         <td>Bar</td>
>     </tr>
> </table>

When moving through an iterator, array, or enumeration
we could inject a special value into the context. Maybe
we might end up with a small set of special values. The
current item in the iteration could be $_item, or $_line.
Just place it in the context while iterating then remove
it when finished.

<table>
    <tr>
      <td>$_item</td>
      <td>Foo</td>
    </tr>
</table>

> How do you easily solve that problem? The only way I can think of right now
> would be to do something like this:
> 
> #foreach $line in $allLines
>     <tr>
>         <td>$line.Number</td>
>         <td>$line.Data</td>
>     </tr>
> #end
> 
> Of course that would require you to create an object ($line) that had the
> information in it and it would mean a fairly large amount of object overhead
> in the generation of the template since $allLines would have to have a new
> Line() object for each line in it. It would be much easier to simply have a
> single String[] above and be able to loop over things.
> 
> hmmm...what about this:
> 
> #foreach $line in $allLines
>     <tr>
>         <td>$line[0]</td>
>         <td>$line[1]</td>
>     </tr>
> #end
> 
> Then $allLines is a String[][] and $line is a String[].
> 
> Hmm...maybe that is the right solution here and we should just forget #math?
> :-)
> 
> comments?

jvz.

-- 

Jason van Zyl
jvanzyl@periapt.com


Re: arithmetic

Posted by Jon Stevens <jo...@latchkey.com>.
on 9/8/2000 1:41 PM, "Eric J Altman" <ea...@speakeasy.org> wrote:

> Something like Sean's Turbine WM utility class to alternate row color is
> much nicer than adding this stuff in anyway, but whatever.
> 
> Regards,
> ~Eric J Altman

The problem is if you want to do something like this:

<table>
    <tr>
        <td>1.</td>
        <td>Foo</td>
    </tr>
    <tr>
        <td>2.</td>
        <td>Bar</td>
    </tr>
</table>

How do you easily solve that problem? The only way I can think of right now
would be to do something like this:

#foreach $line in $allLines
    <tr>
        <td>$line.Number</td>
        <td>$line.Data</td>
    </tr>
#end

Of course that would require you to create an object ($line) that had the
information in it and it would mean a fairly large amount of object overhead
in the generation of the template since $allLines would have to have a new
Line() object for each line in it. It would be much easier to simply have a
single String[] above and be able to loop over things.

hmmm...what about this:

#foreach $line in $allLines
    <tr>
        <td>$line[0]</td>
        <td>$line[1]</td>
    </tr>
#end

Then $allLines is a String[][] and $line is a String[].

Hmm...maybe that is the right solution here and we should just forget #math?
:-)

comments?

-jon

-- 
http://scarab.tigris.org/    | http://noodle.tigris.org/
http://java.apache.org/      | http://java.apache.org/turbine/
http://www.working-dogs.com/ | http://jakarta.apache.org/velocity/
http://www.collab.net/       | http://www.sourcexchange.com/



Re: arithmetic

Posted by Eric J Altman <ea...@speakeasy.org>.
Hi,

Jon Stevens wrote:
> 
> So, start to propose a syntax for implementing this. How about:
> 
> #set $foo = 1
> #math $foo = +1
> Which would take the value of $foo.toString(), convert it into an Integer
> and add one to it. Result = 2

Devil's advocate perhaps, but with this shorthand I can see the web
designer asking why the value of $foo isn't "positive 1" like they
specified. :)  Would #math $foo++ be better?

I'd like just the one verbose format:
> #math $foo = $bar+1
> Which would take the value of $bar.toString(), convert it into an Integer
> and add one to it, but assign the result to $foo. Value of $foo = 2, Value
> of $bar = 1

Something like Sean's Turbine WM utility class to alternate row color is
much nicer than adding this stuff in anyway, but whatever.

Regards,
~Eric J Altman

Re: arithmetic

Posted by Marc Palmer <ma...@anyware.co.uk>.


------ Original Message ------

On 08/09/00, 20:15:55, Jon Stevens <jo...@latchkey.com> wrote regarding Re: 
arithmetic:


> on 9/8/2000 7:28 AM, "Travis Low" <tr...@dawnstar.org> wrote:

> > Is there room in Velocity for integer arithmetic?

> Can we rephrase that as:

> "Is there room in the Template Specification for integer arithmetic?"

> The reason is that I think that before we finalize anything in Velocity
> itself, it should be done through a specification.

For what it's worth, I really don't like this... can it not be achieved 
simply using a helper class, thus keeping the syntax clear and simple, 
with no implicit type conversions etc.?

i.e. Something like this

public class MathHelper {
  
  public Integer increment(Integer i);
  public Integer increment(Integer i, Integer delta);
  public Float increment(Float i);
  public Float increment(Float i, Float delta);

  /* And so on for other simple arithmetic functions ... */
}

In the template usage is obvious:

#set $myvar = $Math.increment( $myvar, 5)

Yeah, for anything except the most trivial it's a bit ugly... but I'm 
from the "keep the logic in the helper classes not the template" school. 
i.e. For the case mentioned (column widths in percentages) I would have 
some $myobject.getColumnWidth( ColumnNum) method.

$0.02

Cheers



Re: arithmetic

Posted by Jon Stevens <jo...@latchkey.com>.
on 9/8/2000 7:28 AM, "Travis Low" <tr...@dawnstar.org> wrote:

> Is there room in Velocity for integer arithmetic?

Can we rephrase that as:

"Is there room in the Template Specification for integer arithmetic?"

The reason is that I think that before we finalize anything in Velocity
itself, it should be done through a specification.

> Designers need it.
> For example, there may be a dynamic number of columns in an HTML
> table, and the designer may wish to specify the width attribute for
> each column as a percentage of the available area.  Or think about
> displaying a calendar month -- padding might be needed for the first
> and last weeks of the month.  Or they may wish to number items in a
> list.  Or display table rows that cycle through three or more colors.
> All of this is currently possible without arithmetic, but it's ugly.

This is something that Jason Hunter wants and I do think it would be helpful
to have for the cases you specify above. See, I'm not going to -1 all
suggestions. :-) 

Although, I'm *really* worried about how to cleanly implement it though
because as soon as you add one thing someone is going want calculus math
next. :-)

So, start to propose a syntax for implementing this. How about:

#set $foo = 1
#math $foo = +1
Which would take the value of $foo.toString(), convert it into an Integer
and add one to it. Result = 2

#set $foo = 1.02
#math $foo = +1
Which would take the value of $foo.toString(), convert it into a Double and
add one to it. Result = 2.02

#set $foo = 5
#set $bar = 1
#math $foo = $bar+1
Which would take the value of $bar.toString(), convert it into an Integer
and add one to it, but assign the result to $foo. Value of $foo = 2, Value
of $bar = 1

When you propose things, you need to start proposing them in a specification
point of view and consider all the different possibilities of how it would
be implemented in the templates.

-jon