You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Peter Romianowski <me...@gmx.de> on 2002/04/17 22:41:30 UTC

macro caching

Hi,

I am doing something quite strange, but I don't know what
to do...
In my project there are templates for several partners (not a
fixed number). Upon HTML-generation I look which partner is
connected and then render the specefic template. That works fine.
Because the design only differs in some general "modules", like
HEADER, FOOTER, LINKDESIGN, TABLEDESIGN and so on, I decided to
do these things with macros. I have multiple templates per partner
and so I put the macros into a design.vm (for each partner, that
wants to override the default design).
Now the strange thing :)
In my Servlet I first render the design-template ignoring its
output and then render the "normal" template. This way I have
access to the macros in my templates. If there would be a better
aproach (#include does not work - it's a pitty...), I would love
to know about it. If there isn't one, then I have the problem,
that the macros defined in design.vm are cached and I have to
restart the whole application (which is really *huge* :) if I
change a template. In the production environment the templates
change dynamically and so the caching is a real problem.

My velocity.properties:

velocimacro.permissions.allow.inline = true
velocimacro.permissions.allow.inline.to.replace.global = true
velocimacro.library.autoreload = true

#----------------------------------------------------------------------------
# T E M P L A T E  L O A D E R S
#----------------------------------------------------------------------------

resource.loader = file

file.resource.loader.description = Velocity File Resource Loader
file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path = ., o:/coreg/projects/promotionserver/runenv,
o:/coreg/projects/promotionserver/runenv/www/WEB-INF/templates,
file.resource.loader.cache = true
file.resource.loader.modificationCheckInterval = 1



Any help is highly appriciated!

Peter


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


Re: macro caching

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
To catch up,  Tommy Svensson also had problems, but it seems that the global
VM approach solves his?

More inline.

On 4/17/02 5:25 PM, "Peter Romianowski" <me...@gmx.de> wrote:

> Hi,
> 
> fast reply - as always! Thanks for that...
> 
>> Ok - there are a few things that jump out as problems.  First, are there
>> multiple 'design.vm' templates?  Or just one that covers it for all
>> partners?
> 
> One for each partner.
> 
>> 
>> Another way : are there multiple
>> 
>> 
>>   #macro( footer $color)
>> 
>> (or whatever)  in the system?
>> 
>> If so, this might be a real problem if all the partner requests are coming
>> through the same webapp.  Why?  What happens when two hit the servlet at the
>> same time?  You might load one set of macros, start rendering, and then the
>> second request comes and loads it's macro set, replacing the first set
>> halfway through.
> 
> Yes! I did not even think of that! But you are absoluty right. So now I have
> a real problem... Besides the caching. A solution would be to synchronize the
> renderer - ehm, not really :)
> Could I use one velocity-instance per partner? That would require some bigger
> code-changes for me - but if it works...

You could, but there may be other ways.

> 
>> There are costs and risks with autoreloading - it's intended only for
>> development.
> 
> The way I would expect things to work is that I simply use a
> #include-Statement.
> But looking at the code I see why this cannot be done so easily, because
> velocity
> is strictly devided into parse-time and run-time, right? Are there any chances
> that 
> this might work in the future? And what should I do until then?

Yes - #parse() won't work as the #parse() is evaluated at run time...

> 
> I am using velocity for almost all types of templating: web, mail, reports and
> so
> on. It really, rellay ROCKS! But I have to say, that some issues are really
> disturbing, 
> like whitespace-gobbling and this one here. I didn't mean to offend you or
> other 
> developers (no way!) - you're doing a great job!

So you are another in the magic whitespace camp?  No offense taken, btw.
Good to know...
 
> So, help or a workaround is still very much appreciated!

Will continue down the thread...

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
Somebody has to do something, and it's just incredibly pathetic that it has
to be us.  - Jerry Garcia


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


RE: java method invoke problem

Posted by Xuan Han <xh...@inktomi.com>.
thanks.  Eventually it's working!

-Xuan

-----Original Message-----
From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
Sent: Wednesday, April 17, 2002 4:05 PM
To: velocity-user@jakarta.apache.org
Subject: Re: java method invoke problem


On 4/17/02 6:02 PM, "Xuan Han" <xh...@inktomi.com> wrote:

> here it is:
>
> import java.net.*;
>
> public class Encoder
> {
>   public String encode (String url)
>   {
>       return ( URLEncoder.encode( url ) );
>   }
> }
>

That looks fine.  I just used it with a template


#set($foo = $encoder.encode("hello there"))
$foo

And the output was

  hello+there

As I expect.

What is output when you do


   $encoder
   $encoder.encode("hello there")



>
> -----Original Message-----
> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
> Sent: Wednesday, April 17, 2002 3:00 PM
> To: velocity-user@jakarta.apache.org
> Subject: Re: java method invoke problem
>
>
> On 4/17/02 5:55 PM, "Xuan Han" <xh...@inktomi.com> wrote:
>
>> I have a public Encoder java object pushed into Context.  It has a
method:
>> public String encode( String).
>>
>> In my vm macro, I invoke method on the Encoder.
>> #set( $valueEncoded = $Encoder.encode($value) )
>>
>> but the value is printed as "Encoder@9f47....", not the encocded string I
>> expected.  What's causing the problem here?  Thanks,
>
> Hard to say. Can we see the Encoder class?
>
> --
> Geir Magnusson Jr.                       geirm@optonline.net
> System and Software Consulting
> You're going to end up getting pissed at your software
> anyway, so you might as well not pay for it. Try Open Source.
>
>
>
> --
> 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>
>

--
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin



--
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: java method invoke problem

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 4/17/02 6:02 PM, "Xuan Han" <xh...@inktomi.com> wrote:

> here it is:
> 
> import java.net.*;
> 
> public class Encoder
> {
>   public String encode (String url)
>   {
>       return ( URLEncoder.encode( url ) );
>   }
> }
> 

That looks fine.  I just used it with a template


#set($foo = $encoder.encode("hello there"))
$foo

And the output was 

  hello+there

As I expect.

What is output when you do


   $encoder
   $encoder.encode("hello there")



> 
> -----Original Message-----
> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
> Sent: Wednesday, April 17, 2002 3:00 PM
> To: velocity-user@jakarta.apache.org
> Subject: Re: java method invoke problem
> 
> 
> On 4/17/02 5:55 PM, "Xuan Han" <xh...@inktomi.com> wrote:
> 
>> I have a public Encoder java object pushed into Context.  It has a method:
>> public String encode( String).
>> 
>> In my vm macro, I invoke method on the Encoder.
>> #set( $valueEncoded = $Encoder.encode($value) )
>> 
>> but the value is printed as "Encoder@9f47....", not the encocded string I
>> expected.  What's causing the problem here?  Thanks,
> 
> Hard to say. Can we see the Encoder class?
> 
> --
> Geir Magnusson Jr.                       geirm@optonline.net
> System and Software Consulting
> You're going to end up getting pissed at your software
> anyway, so you might as well not pay for it. Try Open Source.
> 
> 
> 
> --
> 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>
> 

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin



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


RE: java method invoke problem

Posted by Xuan Han <xh...@inktomi.com>.
here it is:

import java.net.*;

public class Encoder
{
    public String encode (String url)
    {
        return ( URLEncoder.encode( url ) );
    }
}


-----Original Message-----
From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
Sent: Wednesday, April 17, 2002 3:00 PM
To: velocity-user@jakarta.apache.org
Subject: Re: java method invoke problem


On 4/17/02 5:55 PM, "Xuan Han" <xh...@inktomi.com> wrote:

> I have a public Encoder java object pushed into Context.  It has a method:
> public String encode( String).
>
> In my vm macro, I invoke method on the Encoder.
> #set( $valueEncoded = $Encoder.encode($value) )
>
> but the value is printed as "Encoder@9f47....", not the encocded string I
> expected.  What's causing the problem here?  Thanks,

Hard to say. Can we see the Encoder class?

--
Geir Magnusson Jr.                       geirm@optonline.net
System and Software Consulting
You're going to end up getting pissed at your software
anyway, so you might as well not pay for it. Try Open Source.



--
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: java method invoke problem

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 4/17/02 5:55 PM, "Xuan Han" <xh...@inktomi.com> wrote:

> I have a public Encoder java object pushed into Context.  It has a method:
> public String encode( String).
> 
> In my vm macro, I invoke method on the Encoder.
> #set( $valueEncoded = $Encoder.encode($value) )
> 
> but the value is printed as "Encoder@9f47....", not the encocded string I
> expected.  What's causing the problem here?  Thanks,

Hard to say. Can we see the Encoder class?

-- 
Geir Magnusson Jr.                       geirm@optonline.net
System and Software Consulting
You're going to end up getting pissed at your software
anyway, so you might as well not pay for it. Try Open Source.



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


java method invoke problem

Posted by Xuan Han <xh...@inktomi.com>.
I have a public Encoder java object pushed into Context.  It has a method:
public String encode( String).

In my vm macro, I invoke method on the Encoder.
#set( $valueEncoded = $Encoder.encode($value) )

but the value is printed as "Encoder@9f47....", not the encocded string I
expected.  What's causing the problem here?  Thanks,

-Xuan


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


RE: macro caching

Posted by Peter Romianowski <me...@gmx.de>.
Hi,

fast reply - as always! Thanks for that...

> Ok - there are a few things that jump out as problems.  First, are there
> multiple 'design.vm' templates?  Or just one that covers it for all
> partners?

One for each partner.

> 
> Another way : are there multiple
> 
> 
>   #macro( footer $color)
> 
> (or whatever)  in the system?
> 
> If so, this might be a real problem if all the partner requests are coming
> through the same webapp.  Why?  What happens when two hit the servlet at the
> same time?  You might load one set of macros, start rendering, and then the
> second request comes and loads it's macro set, replacing the first set
> halfway through.

Yes! I did not even think of that! But you are absoluty right. So now I have
a real problem... Besides the caching. A solution would be to synchronize the
renderer - ehm, not really :)
Could I use one velocity-instance per partner? That would require some bigger
code-changes for me - but if it works...

> There are costs and risks with autoreloading - it's intended only for
> development.

The way I would expect things to work is that I simply use a #include-Statement.
But looking at the code I see why this cannot be done so easily, because velocity
is strictly devided into parse-time and run-time, right? Are there any chances that 
this might work in the future? And what should I do until then?

I am using velocity for almost all types of templating: web, mail, reports and so
on. It really, rellay ROCKS! But I have to say, that some issues are really disturbing, 
like whitespace-gobbling and this one here. I didn't mean to offend you or other 
developers (no way!) - you're doing a great job!

So, help or a workaround is still very much appreciated!

Thanks,
Peter




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


Re: macro caching

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 4/17/02 4:41 PM, "Peter Romianowski" <me...@gmx.de> wrote:

> Hi,
> 
> I am doing something quite strange, but I don't know what
> to do...
> In my project there are templates for several partners (not a
> fixed number). Upon HTML-generation I look which partner is
> connected and then render the specefic template. That works fine.
> Because the design only differs in some general "modules", like
> HEADER, FOOTER, LINKDESIGN, TABLEDESIGN and so on, I decided to
> do these things with macros. I have multiple templates per partner
> and so I put the macros into a design.vm (for each partner, that
> wants to override the default design).
> Now the strange thing :)
> In my Servlet I first render the design-template ignoring its
> output and then render the "normal" template. This way I have
> access to the macros in my templates. If there would be a better
> aproach (#include does not work - it's a pitty...), I would love
> to know about it. If there isn't one, then I have the problem,
> that the macros defined in design.vm are cached and I have to
> restart the whole application (which is really *huge* :) if I
> change a template. In the production environment the templates
> change dynamically and so the caching is a real problem.

Ok - there are a few things that jump out as problems.  First, are there
multiple 'design.vm' templates?  Or just one that covers it for all
partners?

Another way : are there multiple


  #macro( footer $color)

(or whatever)  in the system?

If so, this might be a real problem if all the partner requests are coming
through the same webapp.  Why?  What happens when two hit the servlet at the
same time?  You might load one set of macros, start rendering, and then the
second request comes and loads it's macro set, replacing the first set
halfway through.

I may have misunderstood you, so this may not be a problem.

> 
> My velocity.properties:
> 
> velocimacro.permissions.allow.inline = true
> velocimacro.permissions.allow.inline.to.replace.global = true
> velocimacro.library.autoreload = true

There are costs and risks with autoreloading - it's intended only for
development.


> 
> #----------------------------------------------------------------------------
> # T E M P L A T E  L O A D E R S
> #----------------------------------------------------------------------------
> 
> resource.loader = file
> 
> file.resource.loader.description = Velocity File Resource Loader
> file.resource.loader.class =
> org.apache.velocity.runtime.resource.loader.FileResourceLoader
> file.resource.loader.path = ., o:/coreg/projects/promotionserver/runenv,
> o:/coreg/projects/promotionserver/runenv/www/WEB-INF/templates,
> file.resource.loader.cache = true
> file.resource.loader.modificationCheckInterval = 1
> 
> 
> 
> Any help is highly appriciated!

Summary of my question : can you re-explain how the design.vm macros work?

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
Java : the speed of Smalltalk with the simple elegance of C++... 


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