You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Terry Steichen <te...@net-frame.com> on 2001/10/17 18:53:24 UTC

Velocity and Javascript

Hope this isn't a silly question, but can Velocity dynamically generate individual lines of Javascript code?  

I know it can spit out an entire Javascript section (<script language=javascript> javascript_stuff </script>, where the parts are constructed ahead of time in Java and then 'put' into the context.

But, what I'd like to do is, inside the javascript block, initialize a varb as thus:

    var sesskey = "$key";

where $key is a Velocity variable.

But it doesn't seem to work.

I'm probably missing something very obvious and simple, but any help would be humbly appreciated.


Regards,

Terry 

Re: Velocity and Javascript

Posted by Nick Bauman <ni...@cortexity.com>.
You should carefully read:

http://jakarta.apache.org/velocity/user-guide.html#String%20Literals

and

http://jakarta.apache.org/velocity/user-guide.html#Parse

> Just thought I'd close out this issue, given the many helpful comments
> I received.
> 
> It was, what we call an 'operator headspace problem.'  Basically, I was
> using a template to pull some javascript code from the context and
> embed it in the page (which worked just fine).  But I tried to get too
> clever, because I had a  Velocity varb (the '$key') included in the
> Javascript. Don't know where my brain was, but I guess I was thinking
> that Velocity can recursively render a template (or something like
> that). Obviously, it can't. Once I put the Javascript where it really
> belongs (in the template), everything worked fine.
> 
> Regards,
> 
> Terry
> 
>> ----- Original Message -----
>> From: "Terry Steichen" <te...@net-frame.com>
>> To: <ve...@jakarta.apache.org>
>> Sent: Wednesday, October 17, 2001 9:53 AM
>> Subject: Velocity and Javascript
>>
>>
>> Hope this isn't a silly question, but can Velocity dynamically
>> generate individual lines of Javascript code?
>>
>> I know it can spit out an entire Javascript section (<script
>> language=javascript> javascript_stuff </script>, where the parts are
>> constructed ahead of time in Java and then 'put' into the context.
>>
>> But, what I'd like to do is, inside the javascript block, initialize a
> varb
>> as thus:
>>
>>     var sesskey = "$key";
>>
>> where $key is a Velocity variable.
>>
>> But it doesn't seem to work.
>>
>> I'm probably missing something very obvious and simple, but any help
>> would be humbly appreciated.
>>
>>
>> Regards,
>>
>> Terry


-- 
Those willing to give up a little freedom for a little bread are likely to 
lose both. --Sidney Crooke, American Abolitionist


Re: Velocity and Javascript

Posted by Terry Steichen <te...@net-frame.com>.
Just thought I'd close out this issue, given the many helpful comments I
received.

It was, what we call an 'operator headspace problem.'  Basically, I was
using a template to pull some javascript code from the context and embed it
in the page (which worked just fine).  But I tried to get too clever,
because I had a  Velocity varb (the '$key') included in the Javascript.
Don't know where my brain was, but I guess I was thinking that Velocity can
recursively render a template (or something like that). Obviously, it can't.
Once I put the Javascript where it really belongs (in the template),
everything worked fine.

Regards,

Terry

> ----- Original Message -----
> From: "Terry Steichen" <te...@net-frame.com>
> To: <ve...@jakarta.apache.org>
> Sent: Wednesday, October 17, 2001 9:53 AM
> Subject: Velocity and Javascript
>
>
> Hope this isn't a silly question, but can Velocity dynamically generate
> individual lines of Javascript code?
>
> I know it can spit out an entire Javascript section (<script
> language=javascript> javascript_stuff </script>, where the parts are
> constructed ahead of time in Java and then 'put' into the context.
>
> But, what I'd like to do is, inside the javascript block, initialize a
varb
> as thus:
>
>     var sesskey = "$key";
>
> where $key is a Velocity variable.
>
> But it doesn't seem to work.
>
> I'm probably missing something very obvious and simple, but any help would
> be humbly appreciated.
>
>
> Regards,
>
> Terry
>
>
>


Re: Velocity and Javascript

Posted by Peter Lynch <pe...@mindspring.com>.
Check to see if that value gets rendered by itself first. Just put

$key

somewhere by itself.

If that doesn't display what you want, then most likely you just don't have
an object name "key" in the template context. View the source after parsing.
Do you see $key or "" with nothing in between?

Velocity does not care about where the context data gets rendered. Although
in some cases formal notation helps velocity understand things better.

 ${key}

-Peter



----- Original Message -----
From: "Terry Steichen" <te...@net-frame.com>
To: <ve...@jakarta.apache.org>
Sent: Wednesday, October 17, 2001 9:53 AM
Subject: Velocity and Javascript


Hope this isn't a silly question, but can Velocity dynamically generate
individual lines of Javascript code?

I know it can spit out an entire Javascript section (<script
language=javascript> javascript_stuff </script>, where the parts are
constructed ahead of time in Java and then 'put' into the context.

But, what I'd like to do is, inside the javascript block, initialize a varb
as thus:

    var sesskey = "$key";

where $key is a Velocity variable.

But it doesn't seem to work.

I'm probably missing something very obvious and simple, but any help would
be humbly appreciated.


Regards,

Terry



Re: Velocity and Javascript

Posted by David Rees <dr...@runt.ebetinc.com>.
On Wed, Oct 17, 2001 at 12:53:24PM -0400, Terry Steichen wrote:
> Hope this isn't a silly question, but can Velocity dynamically generate
> individual lines of Javascript code?
> 
> I know it can spit out an entire Javascript section (<script
> language=javascript> javascript_stuff </script>, where the parts are
> constructed ahead of time in Java and then 'put' into the context.
> 
> But, what I'd like to do is, inside the javascript block, initialize a
> varb as thus:
> 
>     var sesskey = "$key";
> 
> where $key is a Velocity variable.
> 
> But it doesn't seem to work.

It should, I don't see any reason why it wouldn't from your syntax above.

> I'm probably missing something very obvious and simple, but any help would
> be humbly appreciated.

Does $key show up anywhere else in your template?  What exactly do you mean
by "it doesn't seem to work"?

-Dave

Re: Velocity and Javascript

Posted by Saimon Moore <sa...@yahoo.co.uk>.
>     var sesskey = "$key";
>
> where $key is a Velocity variable.
>
> But it doesn't seem to work.
>

You're not giving much infor here (i.e. How doesn't it work? What error 
messages do you get in the velocity log? etc..)

First make sure that $key exists within the context. Checkout velocity.log 
for warning lines about $key not being a reference.

If that's not the problem try using the formal notation ie.

var sesskey = "${key}";

Saimon...

_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com