You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Colin Chalmers <co...@maxware.nl> on 2002/10/21 17:03:09 UTC

Global Variable in Templates

Hi,

I was wondering if it's possible to set a global variable which applies to ALL templates.

For example I want to set a specific Date format which I use in most templates for formatting the presentation of Dates without having to use a #set in all those templates.

Thx

/colin

Re: Global Variable in Templates

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 10/22/02 12:01 PM, "Jonathan Revusky" <re...@wanadoo.es> wrote:

> On Monday 21 October 2002 05:03 pm, Colin Chalmers wrote:
>> Hi,
>> 
>> I was wondering if it's possible to set a global variable which applies to
>> ALL templates.
> 
> <deep breath>
> 
> Apparently, there is no explicit way to do this in Velocity. You simply have
> to make sure that the variable is placed in the context each time.
> 
> This feature does exist in FreeMarker -- as of FreeMarker 2.1.
> 
> There is the object freemarker.template.Configuration which represents all the
> configuration settings -- it's pretty much analogous with what is called the
> "engine" in Velocity-land.
> 
> Anyway, it is:
> 
> Configuration config = Configuration.getDefaultConfiguration();
> 
> This returns the configuration object that is used by default. Though there is
> the option of there being more than one Configuration object within a
> FreeMarker application, that usage is probably rare. So, for most typical
> usages, the Configuration is a singleton.
> 
> In any case, with a Configuration object, you can do:
> 
> config.setSharedVariable("foo", "bar");
> 
> and basically, all the templates will have a "foo" variable set that is equal
> to "bar".


Hey, that sounds like like the toolbox we have done everywhere in vel-tools,
like the tool manager stuff in Webmacro, and the notion of the tool
configuration in Turbine.

Yet Another Implementation of the Same Idea certainly validates the pattern.
 


-- 
Geir Magnusson Jr. 
geirm@adeptra.com                                    +1-203-355-2219 (w)
Adeptra Inc.                                         +1-203-247-1713 (m)



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


Re: Global Variable in Templates

Posted by Jonathan Revusky <re...@wanadoo.es>.
On Monday 21 October 2002 05:03 pm, Colin Chalmers wrote:
> Hi,
>
> I was wondering if it's possible to set a global variable which applies to
> ALL templates.

<deep breath>

Apparently, there is no explicit way to do this in Velocity. You simply have 
to make sure that the variable is placed in the context each time.

This feature does exist in FreeMarker -- as of FreeMarker 2.1.

There is the object freemarker.template.Configuration which represents all the 
configuration settings -- it's pretty much analogous with what is called the 
"engine" in Velocity-land.

Anyway, it is:

Configuration config = Configuration.getDefaultConfiguration();

This returns the configuration object that is used by default. Though there is 
the option of there being more than one Configuration object within a 
FreeMarker application, that usage is probably rare. So, for most typical 
usages, the Configuration is a singleton.

In any case, with a Configuration object, you can do:

config.setSharedVariable("foo", "bar");

and basically, all the templates will have a "foo" variable set that is equal 
to "bar".

>
> For example I want to set a specific Date format which I use in most
> templates for formatting the presentation of Dates without having to use a
> #set in all those templates.

In FreeMarker, the Configuration object allows you to set default date formats 
that will be used transparently when displaying time/date objects.

config.setDateFormat("HH:MM");

or something like that, and all time/date objects will be displayed (by 
default) with that format.

Of course, the general setting in the Configuration object can be overridden 
at the template level.

Well, I hope that's helpful. I think this information could be useful to you.

Jonathan Revusky
--
FreeMarker 2.1 is out! http://freemarker.sourceforge.net/
FreeMarker-Velocity comparison doc
http://freemarker.sourceforge.net/fmVsVel.html
Velocity->FreeMarker template conversion utility
http://freemarker.sourceforge.net/usCavalry.html


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


Re: Global Variable in Templates

Posted by Nathan Bubna <na...@esha.com>.
Claude said:
> And in a webapp environment, the easiest way to do this is to overload the
createContext method, in the VelocityServlet or
> VelocityViewServlet class (and to call super.createContext() to create
your personnalized context)

actually, you shouldn't even need to do this much with the
VelocityViewServlet.  you can use the toolbox support to automatically add &
configure both string, boolean, or numeric constants or any java class with
a public default constructor by an xml file.

e.g.

<toolbox>
   <tool>
      <key>math</key>
      <scope>application</scope>
      <class>org.apache.velocity.tools.tools.MathTool</class>
   </tool>
   <data type="Number">
      <key>luckynumber</key>
      <value>1.37</class>
   </data>
   <data type="String">
      <key>greeting</key>
      <value>Hello World!</class>
   </data>
 </toolbox>

i'm sorry these things haven't been well-documented yet, but there are some
good-yet-incomplete docs at

http://www.teamup.com/jakarta-velocity-tools/docs/index.html

looking at the examples in cvs and especially in the javadoc may help too.

http://cvs.apache.org/viewcvs/jakarta-velocity-tools

http://www.teamup.com/jakarta-velocity-tools/view/docs/javadoc-velservlet/in
dex.html

of particular use to you should be the ServletToolboxManager class.

if you are looking to do webapps with velocity, i think you'll find the
VelocityViewServlet and friends to come in quite handy.  they certainly have
for me.

Nathan Bubna
nathan@esha.com


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


Re: Global Variable in Templates

Posted by Claude Brisson <cl...@savoirweb.com>.
> On 10/21/02 11:03 AM, "Colin Chalmers" <co...@maxware.nl> wrote:
>
> > Hi,
> >
> > I was wondering if it's possible to set a global variable which applies to ALL
> > templates.
> >
> > For example I want to set a specific Date format which I use in most templates
> > for formatting the presentation of Dates without having to use a #set in all
> > those templates.
>
> The only way to do that is to put it into the context for all templates.
> This is a common practice, putting common values, tools and such into the
> context.  It also leads to the so-called 'pull' model, where there are tools
> that you can use to access the data bits you need, rather than have the code
> put it into the context for you.
>
> geir

And in a webapp environment, the easiest way to do this is to overload the createContext method, in the VelocityServlet or
VelocityViewServlet class (and to call super.createContext() to create your personnalized context)

In a servlet en

CloD
----- Original Message -----
From: "Geir Magnusson Jr." <ge...@adeptra.com>
To: <ve...@jakarta.apache.org>
Sent: lundi 21 octobre 2002 20:49
Subject: Re: Global Variable in Templates


> On 10/21/02 11:03 AM, "Colin Chalmers" <co...@maxware.nl> wrote:
>
> > Hi,
> >
> > I was wondering if it's possible to set a global variable which applies to ALL
> > templates.
> >
> > For example I want to set a specific Date format which I use in most templates
> > for formatting the presentation of Dates without having to use a #set in all
> > those templates.
>
> The only way to do that is to put it into the context for all templates.
> This is a common practice, putting common values, tools and such into the
> context.  It also leads to the so-called 'pull' model, where there are tools
> that you can use to access the data bits you need, rather than have the code
> put it into the context for you.
>
> geir
> --
> Geir Magnusson Jr.
> geirm@adeptra.com                                    +1-203-355-2219 (w)
> Adeptra Inc.                                         +1-203-247-1713 (m)
>
>
>
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>


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


Re: Global Variable in Templates

Posted by "Geir Magnusson Jr." <ge...@adeptra.com>.
On 10/21/02 11:03 AM, "Colin Chalmers" <co...@maxware.nl> wrote:

> Hi,
> 
> I was wondering if it's possible to set a global variable which applies to ALL
> templates.
> 
> For example I want to set a specific Date format which I use in most templates
> for formatting the presentation of Dates without having to use a #set in all
> those templates.

The only way to do that is to put it into the context for all templates.
This is a common practice, putting common values, tools and such into the
context.  It also leads to the so-called 'pull' model, where there are tools
that you can use to access the data bits you need, rather than have the code
put it into the context for you.

geir
-- 
Geir Magnusson Jr. 
geirm@adeptra.com                                    +1-203-355-2219 (w)
Adeptra Inc.                                         +1-203-247-1713 (m)



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