You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Robert Koberg <ro...@koberg.com> on 2005/08/23 19:01:06 UTC

set a value in a macro

Hi,

Say I have a VM_global_library.vm that has a macro like:

#macro (roleList)

   #set ($roles = ["foo", "bar"])

#end

Should I be able to do the following in a template:

#roleList
#foreach ($role in $roles)
   ...
#end


OR, how would you globally set a variable that is only needed in a few 
different templates? In other words, I was hoping to instantiate the 
$roles variable only when needed?

Any  ideas?

best,
-Rob

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


Re: set a value in a macro

Posted by Barbara Baughman <ba...@utdallas.edu>.
I personally set all of my global variables whenever I touch Velocity,
and I have at least 100 of them, some defined with some pretty
complicated routines.  It doesn't seem to impact response times on the
web application, and it saves a lot of programming and debugging time
to just know they are always there.

I'm very impressed with Velocity's speed.  I'm willing to trade minor
inefficiency for speed in coding.

Barbara Baughman
X2157

On Tue, 23 Aug 2005, Robert Koberg wrote:

> Hi,
>
> Say I have a VM_global_library.vm that has a macro like:
>
> #macro (roleList)
>
>    #set ($roles = ["foo", "bar"])
>
> #end
>
> Should I be able to do the following in a template:
>
> #roleList
> #foreach ($role in $roles)
>    ...
> #end
>
>
> OR, how would you globally set a variable that is only needed in a few
> different templates? In other words, I was hoping to instantiate the
> $roles variable only when needed?
>
> Any  ideas?
>
> best,
> -Rob
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>

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


Re: set a value in a macro

Posted by Nathan Bubna <nb...@gmail.com>.
On 8/25/05, Robert Koberg <ro...@koberg.com> wrote:
> Nathan Bubna wrote:
> 
> > hmm.  both your way and Jason's?
> 
> No, I did not try to #parse. Just tried to set the variable value in a
> macro.  haven't needed to #parse anything yet - I guess I could look
> into it.
> 
> 
> > also, what were your velocimacro
> > settings in your velocity.properties? (just curious, i'm not sure if
> > they'll make a difference)
> >
> 
> Here is my velocity.properties:
> 
> input.encoding = UTF-8
> output.encoding = UTF-8
> 
> webapp.resource.loader.cache = false
> velocimacro.library.autoreload = true
> 
> velocimacro.library = /WEB-INF/VM_global_library.vm

yeah, if it were going to work, it should have done so with those. 
the only other thing you might try (besides Jason's #parse method, of
course) would be to do a #set( $roles = 'foo' ) before the #roleList()
macro call.  that might make the $roles variable stick.

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


Re: set a value in a macro

Posted by Robert Koberg <ro...@koberg.com>.
Nathan Bubna wrote:

> hmm.  both your way and Jason's?  

No, I did not try to #parse. Just tried to set the variable value in a 
macro.  haven't needed to #parse anything yet - I guess I could look 
into it.


> also, what were your velocimacro
> settings in your velocity.properties? (just curious, i'm not sure if
> they'll make a difference)
> 

Here is my velocity.properties:

input.encoding = UTF-8
output.encoding = UTF-8

webapp.resource.loader.cache = false
velocimacro.library.autoreload = true

velocimacro.library = /WEB-INF/VM_global_library.vm

thanks,
-Rob

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


Re: set a value in a macro

Posted by Nathan Bubna <nb...@gmail.com>.
On 8/25/05, Robert Koberg <ro...@koberg.com> wrote:
> Nathan Bubna wrote:
> 
> >>>
> >>>>OR, how would you globally set a variable that is only needed in a few
> >>>>different templates? In other words, I was hoping to instantiate the
> >>>>$roles variable only when needed?
> >>>>
> >>>>Any  ideas?
> >>>
> >>>
> >>>if you use the VelocityViewServlet (part of VelocityTools), you could
> >>>set some data (no arrays/lists yet, sorry) in the toolbox.xml as
> >>><data> elements.  or you could use a servlet filter to put the values
> >>>in the servletContext attributes upon first request.  then they'll be
> >>>automatically available to all templates and subsequent requests.
> >>
> >>Hi,
> >>thanks, but I was looking for a way to keep it available, but not put it
> >>into memory unless I needed it.
> >>
> >>It is not a bug deal (the array contains 11 items...), just checking.
> >
> >
> > ah.  i didn't pick up on that.  so, having once again skimmed thru
> > your first email, i'm curious...  did you try the velocimacro approach
> > you described?  i think it ought to work.  Jason's method should too.
> 
> I did try it and it did not work.

hmm.  both your way and Jason's?  also, what were your velocimacro
settings in your velocity.properties? (just curious, i'm not sure if
they'll make a difference)

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


Re: set a value in a macro

Posted by Robert Koberg <ro...@koberg.com>.
Nathan Bubna wrote:

>>>
>>>>OR, how would you globally set a variable that is only needed in a few
>>>>different templates? In other words, I was hoping to instantiate the
>>>>$roles variable only when needed?
>>>>
>>>>Any  ideas?
>>>
>>>
>>>if you use the VelocityViewServlet (part of VelocityTools), you could
>>>set some data (no arrays/lists yet, sorry) in the toolbox.xml as
>>><data> elements.  or you could use a servlet filter to put the values
>>>in the servletContext attributes upon first request.  then they'll be
>>>automatically available to all templates and subsequent requests.
>>
>>Hi,
>>thanks, but I was looking for a way to keep it available, but not put it
>>into memory unless I needed it.
>>
>>It is not a bug deal (the array contains 11 items...), just checking.
> 
> 
> ah.  i didn't pick up on that.  so, having once again skimmed thru
> your first email, i'm curious...  did you try the velocimacro approach
> you described?  i think it ought to work.  Jason's method should too.

I did try it and it did not work.

best,
-Rob


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


Re: set a value in a macro

Posted by Nathan Bubna <nb...@gmail.com>.
On 8/24/05, Robert Koberg <ro...@koberg.com> wrote:
> Nathan Bubna wrote:
> > On 8/23/05, Robert Koberg <ro...@koberg.com> wrote:
> > #snip
> >
> >>OR, how would you globally set a variable that is only needed in a few
> >>different templates? In other words, I was hoping to instantiate the
> >>$roles variable only when needed?
> >>
> >>Any  ideas?
> >
> >
> > if you use the VelocityViewServlet (part of VelocityTools), you could
> > set some data (no arrays/lists yet, sorry) in the toolbox.xml as
> > <data> elements.  or you could use a servlet filter to put the values
> > in the servletContext attributes upon first request.  then they'll be
> > automatically available to all templates and subsequent requests.
> 
> Hi,
> thanks, but I was looking for a way to keep it available, but not put it
> into memory unless I needed it.
> 
> It is not a bug deal (the array contains 11 items...), just checking.

ah.  i didn't pick up on that.  so, having once again skimmed thru
your first email, i'm curious...  did you try the velocimacro approach
you described?  i think it ought to work.  Jason's method should too.

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


Re: set a value in a macro

Posted by Robert Koberg <ro...@koberg.com>.
Nathan Bubna wrote:
> On 8/23/05, Robert Koberg <ro...@koberg.com> wrote:
> #snip 
> 
>>OR, how would you globally set a variable that is only needed in a few
>>different templates? In other words, I was hoping to instantiate the
>>$roles variable only when needed?
>>
>>Any  ideas?
> 
> 
> if you use the VelocityViewServlet (part of VelocityTools), you could
> set some data (no arrays/lists yet, sorry) in the toolbox.xml as
> <data> elements.  or you could use a servlet filter to put the values
> in the servletContext attributes upon first request.  then they'll be
> automatically available to all templates and subsequent requests.

Hi,
thanks, but I was looking for a way to keep it available, but not put it 
into memory unless I needed it.

It is not a bug deal (the array contains 11 items...), just checking.

best,
-Rob

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


Re: set a value in a macro

Posted by Nathan Bubna <nb...@gmail.com>.
On 8/23/05, Robert Koberg <ro...@koberg.com> wrote:
#snip 
> OR, how would you globally set a variable that is only needed in a few
> different templates? In other words, I was hoping to instantiate the
> $roles variable only when needed?
> 
> Any  ideas?

if you use the VelocityViewServlet (part of VelocityTools), you could
set some data (no arrays/lists yet, sorry) in the toolbox.xml as
<data> elements.  or you could use a servlet filter to put the values
in the servletContext attributes upon first request.  then they'll be
automatically available to all templates and subsequent requests.

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


Re: set a value in a macro

Posted by Jason Pettiss <ja...@TheCatalis.com>.
I'm not sure about the performance impact of this, but I just create a 
template which has my common variables for a particular area of my app 
and #parse it from wherever I need it. 

--jason

Robert Koberg wrote:

> Hi,
>
> Say I have a VM_global_library.vm that has a macro like:
>
> #macro (roleList)
>
>   #set ($roles = ["foo", "bar"])
>
> #end
>
> Should I be able to do the following in a template:
>
> #roleList
> #foreach ($role in $roles)
>   ...
> #end
>
>
> OR, how would you globally set a variable that is only needed in a few 
> different templates? In other words, I was hoping to instantiate the 
> $roles variable only when needed?
>
> Any  ideas?
>
> best,
> -Rob
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>


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