You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by David Esposito <es...@newnetco.com> on 2001/05/14 17:34:34 UTC

RE: multiple resource paths

We sort of do this in our application ... However, this depends on all of
your templates being under one common "root" directory ... if you have them
across different disks, then you're going to need a more complex solution:

try{
	templateName = templateBaseDir + "/xml/" + outTemplate.getTemplateName();
	outty = getTemplate(templateName);
}
catch(ResourceNotFoundException rnfe){
	log("Could not find XML version of template; defaulting to HTML!");
	templateName = templateBaseDir + "/html/" + outTemplate.getTemplateName();
	outty = getTemplate(templateName);
}

where templateName could be something like "/base/client1"  ... you can use
relative paths in your call to getTemplate ...

-Dave

> -----Original Message-----
> From: Simon Christian [mailto:simon@cpd.co.uk]
> Sent: Monday, May 14, 2001 6:54 PM
> To: velocity-user@jakarta.apache.org
> Subject: multiple resource paths
>
>
> Hi all,
>
> We're currently evaluating Velocity with a view to replacing our current
> template system (in fact the venerable java apache java-utils template
> system).
>
> One facet of our current arrangement is to have a set of 'base'
> templates, and then optional bespoke templates for each of 100+ clients.
> If a bespoke template exists it is used, otherwise the base one is used.
> All the client templates reside under one directory so for example:
>
> 	base templates in /base/templates
> 	client templates in /base/templates/client1, /base/templates/client2
> etc
>
> A nice part of this arrangement is that we can simply drop in a new
> template to a client difectory and the bespoke look is in effect.
> However this looks difficult to achieve with Velocity - we could provide
> 100+ resource locations in the configuration file but firstly this seems
> very inefficient, and secondly doesn't really work for us either - it
> should only try in two locations in any once instance.
>
> One way I'm thinking of circumventing the problem (I really want to use
> Velocity) is to write a new resource loader which handles this case.
> Might it be reasonable to provide the client name as part of the
> resource source with which getResourceStream is called (e.g.
> 'client1:admin/listusers.vm'), and then split and attempt to find the
> appropriate resource?
>
> Alternatively, does anyone have a better suggestion...?
>
> Thanks,
>
> - simon


Re: Stream over template?

Posted by "Kyle F. Downey" <kd...@amberarcher.com>.
Jacek Kempski wrote:

> Hello,
> 
> how can i send a dynamically created image based on data retrieved from 
> the database? Do i have to save the image temporarily on the HD and pass 
> the URL to the template? Or is there another possibility ?
> 

Why not embed a URL that points directly to the servlet reading the data from
the database?

img src="/servlet/DataSource" 

so long as that servlet puts the right header on it (image/gif or image/jpeg)
it should work. There's nothing template-specific about that.....

--kd

> Thanks to everyone, special thanks to Geir for the previous help.
> 
> jack.
> 
> jk@jacekkempski.com
> jacek@camline.com



Re: Stream over template?

Posted by Jon Stevens <jo...@latchkey.com>.
Turn off that html email please.

on 5/14/01 11:56 AM, "Jacek Kempski" <jk...@jacekkempski.com> wrote:

> Hello, 
> 
> how can i send a dynamically created image based on data retrieved from the
> database? Do i have to save the image temporarily on the HD and pass the URL
> to the template? Or is there another possibility ?

You can do either of both. You can put the image data byte[] into the
context and have a template which only contains a reference to the context
variable or you can save the image to disk within your htdocs and reference
it in your template or you can just have the url point to a servlet which
then returns the image (no need for velocity overhead).

-jon

-- 
If you come from a Perl or PHP background, JSP is a way to take
your pain to new levels. --Anonymous
<http://jakarta.apache.org/velocity/ymtd/ymtd.html>


Stream over template?

Posted by Jacek Kempski <jk...@jacekkempski.com>.
Hello,

how can i send a dynamically created image based on data retrieved from the database? Do
i have to save the image temporarily on the HD and pass the URL to the template? Or is
there another possibility ?

Thanks to everyone, special thanks to Geir for the previous help.

jack.

jk@jacekkempski.com
jacek@camline.com

RE: multiple resource paths

Posted by David Duddleston <da...@i2a.com>.

> -----Original Message-----
> From: Simon Christian [mailto:simon@cpd.co.uk]
>
> The only situation where this doesn't resolve the problem is when using
> #include or #parse inside the templates. If each template had a #parse(
> "navigation.vm" ) of some form at the top, this couldn't be made
> 'cascading' in the same fashion - the templates would have to define
> where the navigation template was to be found e.g. #parse(
> "/base/client1/navigation.vm" ).

I'm thinking an event handler for #include() and #parse(), could be useful
and help solve your problem along with one of mine. I don't know how easy
this would be to add to Velocity.

-david


Re: multiple resource paths

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
David Esposito wrote:
> 
> aha! we thought of that too! .. ;) ..
> 
> I kinda simplified the code a bit that i sent along in that example, but
> after that try/catch block, we use the "templateDir" (whichever one was
> successfully loaded) and stick that onto the context ... something like
> 
> ctx.put("templateDir",templateDir);
> 
> then in ALL of our calls to #parse or #include, we do
> 
> #parse("$!templateDir/include/header.vm")

Another approach would be a context tool :

  #parse( $dirTool.getTemplate( "header.vm" ) )

which would do the right thing.



-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
"still climbing up to the shoulders..."

RE: multiple resource paths

Posted by David Esposito <es...@newnetco.com>.
aha! we thought of that too! .. ;) ..

I kinda simplified the code a bit that i sent along in that example, but
after that try/catch block, we use the "templateDir" (whichever one was
successfully loaded) and stick that onto the context ... something like

ctx.put("templateDir",templateDir);

then in ALL of our calls to #parse or #include, we do

#parse("$!templateDir/include/header.vm")

or something like that ... (if the templateDir wasn't put onto the context,
then it just tries to load "/include/header.vm") ... it does require some
discipline to make sure that all of your calls to #parse and #include are
appropriately prefixed ... you might want to put your "default" templates
into a subdirectory off of your root (and put the appropriate value of
templateDir onto your Context) so that you can pick up on missing
$templateDirs in your templates ..

the one last sticky point is Velocimacro files ... i don't think you'll be
able to use this trick if you want different macros loaded based on the
directory you're in ... we just have one copy of our macro file in the
"base" (file.resource.loader.path) directory ...

-Dave

> -----Original Message-----
> From: Simon Christian [mailto:simon@cpd.co.uk]
> Sent: Monday, May 14, 2001 8:43 PM
> To: velocity-user@jakarta.apache.org
> Subject: Re: multiple resource paths
>
>
> Thanks Dave you've pullded the wool from my eyes!
>
> This is in fact very similar to the method we're currently using (duh).
>
> The only situation where this doesn't resolve the problem is when using
> #include or #parse inside the templates. If each template had a #parse(
> "navigation.vm" ) of some form at the top, this couldn't be made
> 'cascading' in the same fashion - the templates would have to define
> where the navigation template was to be found e.g. #parse(
> "/base/client1/navigation.vm" ).
>
> I know I'm being picky here, but is this an area worth (my) delving
> deeper into?
>
> - simon
>
> David Esposito wrote:
> >
> > We sort of do this in our application ... However, this depends
> on all of
> > your templates being under one common "root" directory ... if
> you have them
> > across different disks, then you're going to need a more
> complex solution:
> >
> > try{
> >         templateName = templateBaseDir + "/xml/" +
> outTemplate.getTemplateName();
> >         outty = getTemplate(templateName);
> > }
> > catch(ResourceNotFoundException rnfe){
> >         log("Could not find XML version of template; defaulting
> to HTML!");
> >         templateName = templateBaseDir + "/html/" +
> outTemplate.getTemplateName();
> >         outty = getTemplate(templateName);
> > }
> >
> > where templateName could be something like "/base/client1"  ...
> you can use
> > relative paths in your call to getTemplate ...
> >
> > -Dave
> >
> > > -----Original Message-----
> > > From: Simon Christian [mailto:simon@cpd.co.uk]
> > > Sent: Monday, May 14, 2001 6:54 PM
> > > To: velocity-user@jakarta.apache.org
> > > Subject: multiple resource paths
> > >
> > >
> > > Hi all,
> > >
> > > We're currently evaluating Velocity with a view to replacing
> our current
> > > template system (in fact the venerable java apache java-utils template
> > > system).
> > >
> > > One facet of our current arrangement is to have a set of 'base'
> > > templates, and then optional bespoke templates for each of
> 100+ clients.
> > > If a bespoke template exists it is used, otherwise the base
> one is used.
> > > All the client templates reside under one directory so for example:
> > >
> > >       base templates in /base/templates
> > >       client templates in /base/templates/client1,
> /base/templates/client2
> > > etc
> > >
> > > A nice part of this arrangement is that we can simply drop in a new
> > > template to a client difectory and the bespoke look is in effect.
> > > However this looks difficult to achieve with Velocity - we
> could provide
> > > 100+ resource locations in the configuration file but firstly
> this seems
> > > very inefficient, and secondly doesn't really work for us either - it
> > > should only try in two locations in any once instance.
> > >
> > > One way I'm thinking of circumventing the problem (I really
> want to use
> > > Velocity) is to write a new resource loader which handles this case.
> > > Might it be reasonable to provide the client name as part of the
> > > resource source with which getResourceStream is called (e.g.
> > > 'client1:admin/listusers.vm'), and then split and attempt to find the
> > > appropriate resource?
> > >
> > > Alternatively, does anyone have a better suggestion...?
> > >
> > > Thanks,
> > >
> > > - simon


Re: multiple resource paths

Posted by Simon Christian <si...@cpd.co.uk>.
Thanks Dave you've pullded the wool from my eyes!

This is in fact very similar to the method we're currently using (duh).

The only situation where this doesn't resolve the problem is when using
#include or #parse inside the templates. If each template had a #parse(
"navigation.vm" ) of some form at the top, this couldn't be made
'cascading' in the same fashion - the templates would have to define
where the navigation template was to be found e.g. #parse(
"/base/client1/navigation.vm" ). 

I know I'm being picky here, but is this an area worth (my) delving
deeper into?

- simon

David Esposito wrote:
> 
> We sort of do this in our application ... However, this depends on all of
> your templates being under one common "root" directory ... if you have them
> across different disks, then you're going to need a more complex solution:
> 
> try{
>         templateName = templateBaseDir + "/xml/" + outTemplate.getTemplateName();
>         outty = getTemplate(templateName);
> }
> catch(ResourceNotFoundException rnfe){
>         log("Could not find XML version of template; defaulting to HTML!");
>         templateName = templateBaseDir + "/html/" + outTemplate.getTemplateName();
>         outty = getTemplate(templateName);
> }
> 
> where templateName could be something like "/base/client1"  ... you can use
> relative paths in your call to getTemplate ...
> 
> -Dave
> 
> > -----Original Message-----
> > From: Simon Christian [mailto:simon@cpd.co.uk]
> > Sent: Monday, May 14, 2001 6:54 PM
> > To: velocity-user@jakarta.apache.org
> > Subject: multiple resource paths
> >
> >
> > Hi all,
> >
> > We're currently evaluating Velocity with a view to replacing our current
> > template system (in fact the venerable java apache java-utils template
> > system).
> >
> > One facet of our current arrangement is to have a set of 'base'
> > templates, and then optional bespoke templates for each of 100+ clients.
> > If a bespoke template exists it is used, otherwise the base one is used.
> > All the client templates reside under one directory so for example:
> >
> >       base templates in /base/templates
> >       client templates in /base/templates/client1, /base/templates/client2
> > etc
> >
> > A nice part of this arrangement is that we can simply drop in a new
> > template to a client difectory and the bespoke look is in effect.
> > However this looks difficult to achieve with Velocity - we could provide
> > 100+ resource locations in the configuration file but firstly this seems
> > very inefficient, and secondly doesn't really work for us either - it
> > should only try in two locations in any once instance.
> >
> > One way I'm thinking of circumventing the problem (I really want to use
> > Velocity) is to write a new resource loader which handles this case.
> > Might it be reasonable to provide the client name as part of the
> > resource source with which getResourceStream is called (e.g.
> > 'client1:admin/listusers.vm'), and then split and attempt to find the
> > appropriate resource?
> >
> > Alternatively, does anyone have a better suggestion...?
> >
> > Thanks,
> >
> > - simon