You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Erik Mattsson <er...@imbridge.com> on 2002/04/17 10:25:49 UTC

How to handle the $int.valueOf()!?

Hi

Ive just started using velocity, and fins it quite nice to part the logic
from the
presentation. Ive designed a webpage for displaying images. Since I want to
divide up
the images in several pages (not all in one page), then it would be nice to
get links to the
other pages ( something like 1,2,3,5,6,7).. For this I have the number of
pages
and the active page nr, but I cant get $int.valueOf() to work.

Im even having problems running one of the examples in
http://jakarta.apache.org/velocity/user-guide.html
page. When I insert the example (below) into my template, the output is not
what it should be.

"#set($a = "7")
#set($b = $int.valueOf($a) + 10)
$b"

The displayed output is "$b" and not "17" as the example states.

Ive tried both the velocity 1.3 and 1.2, with similar result, is there
anything that I must do
before the parsing of the integer, or is it just a bug ??

Any help is appreciated !!

Regards
Erik


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


Re: How to handle the $int.valueOf()!?

Posted by Ben Peter <bp...@zentropypartners.com>.
Erik Mattsson wrote:

> "#set($a = "7")
> #set($b = $int.valueOf($a) + 10)
> $b"

I would do the following:

#set($a = 7)
#set($b = 10 + $a)
$b

Although I do net see why the above example should give you
$b as the output unless $b itself is part of some other surrounding 
circumstances (like a parameter of a macro passed to another).

hth,
Ben
-- 
Benjamin Peter                                          +49-69-96244395
Application Engineer                             Moerfelder Landstr. 55
(zentropy:partners)                            60598 Frankfurt, Germany


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


Re: How to handle the $int.valueOf()!?

Posted by Ben Peter <bp...@zentropypartners.com>.
Erik Mattsson wrote:

> "#set($a = "7")
> #set($b = $int.valueOf($a) + 10)
> $b"

And: have a look at you velocity log. most certainly you will find for the 2nd 
line in this example that the RHS (right hand side) of the set statement cannot 
be evaluated, perhaps because there is no $int.

This, of course, will let the value of $b at what it was (most probably null) 
and the output in the third line will render $b. Use $!b if you want to 
suppress output in that case.

Ben
-- 
Benjamin Peter                                          +49-69-96244395
Application Engineer                             Moerfelder Landstr. 55
(zentropy:partners)                            60598 Frankfurt, Germany


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


Re: How to handle the $int.valueOf()!?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 4/17/02 4:25 AM, "Erik Mattsson" <er...@imbridge.com> wrote:

> Hi
> 
> Ive just started using velocity, and fins it quite nice to part the logic
> from the
> presentation. Ive designed a webpage for displaying images. Since I want to
> divide up
> the images in several pages (not all in one page), then it would be nice to
> get links to the
> other pages ( something like 1,2,3,5,6,7).. For this I have the number of
> pages
> and the active page nr, but I cant get $int.valueOf() to work.
> 
> Im even having problems running one of the examples in
> http://jakarta.apache.org/velocity/user-guide.html
> page. When I insert the example (below) into my template, the output is not
> what it should be.
> 
> "#set($a = "7")
> #set($b = $int.valueOf($a) + 10)
> $b"
> 
> The displayed output is "$b" and not "17" as the example states.

Someone else answered, but I am going to do also just as a recap.

First, the text surrounding the example is just plain wrong. Sorry about
that.  Will be fixed now.  Baffling actually.

#set() can set anything.  It is not limited to strings

#set($a = 7)
#set($a = '7')
#set($a = 'seven')
#set($a = [1..10])
#set($a = $foo.bar())

First sets the reference 'a' to an Integer, second and third to strings, and
the fourth, which we call the 'range operator' is really an ArrayList
containing the Integers 1->10 inclusive.  The fifth sets it to whatever the
public bar() method of the object stored under the key 'foo' in the context
returns.

You get the idea.

So to do addition :

#set($a = 7)
#set($b = $a + 10)

Works just fine

In the event you *do* have a string you want to add to an integer, you do
need to use what we call a 'context tool' or 'tool' for short.

You can formally put into the context an Integer, and then use any of the
integer's methods, for example.  Suppose you did put an Integer into the
context as 'int' :

  context.put("int", new Integer(0));

Then you could use it as :

  #set($a = "8")

  #set($b = $int.valueOf($a) + 2)

Making $b == 10

Anyway, hope that clears it up...




-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"We will be judged not by the monuments we build, but by the monuments we
destroy" - Ada Louise Huxtable


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