You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Jonas Bengtsson <jo...@home.se> on 2001/11/02 00:45:11 UTC

Multiple templates in servlets

Hi,
<newbie-quiestion-alert>
Is it possible to use several templates in one servlet. For instance if I
have three templates called: header.vm, content.vm and footer.vm. Is it
possible to use all these templates from the servlet, i.e. not with #parse
directives.
</newbie-question-alert>

Thanks in advance,
Jonas Bengtsson


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


Re: Multiple templates in servlets

Posted by Dan Bachelder <ch...@chowda.net>.
I know you said no #parse... but had you considered the following?

you could use a forth page "page.vm" which could just be

#parse("header.vm")
#parse("content.vm")
#parse("footer.vm")

----- Original Message -----
From: "Jonas Bengtsson" <jo...@home.se>
To: <ve...@jakarta.apache.org>
Sent: Thursday, November 01, 2001 6:45 PM
Subject: Multiple templates in servlets


> Hi,
> <newbie-quiestion-alert>
> Is it possible to use several templates in one servlet. For instance if I
> have three templates called: header.vm, content.vm and footer.vm. Is it
> possible to use all these templates from the servlet, i.e. not with #parse
> directives.
> </newbie-question-alert>
>
> Thanks in advance,
> Jonas Bengtsson
>
>
> --
> 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: Multiple templates in servlets

Posted by LS <al...@yahoo.com>.
Jonas-
    It looks like what you are asking for is what some folks call a web development
framework. I asked a similar question to yours on this list a few weeks ago, and
was pointed at Maverick and Turbine. I'm been using Maverick since then, and it's
just what I wanted, without anything that I don't want. You might want to check it
out.




--- Jonas Bengtsson <jo...@home.se> wrote:
> Yes you have understood me correctly.
> Would it require much rework?
> 
> One thing would be to reduce the number of files.
> Another problem with a single template is, for instance, when one has a
> plug-in page:
> * each plug-in will generate some output to the page
> * there is no way of knowing which plug-ins there are
> If there would be a parseTemplate() method one could do like this:
> parseTemplate(getTemplate("header.vm"));
> for(int i=0;i<plugIns.length;i++) {
>    parseTemplate(plugIns[i].generateTemplate(context));
> }
> parseTemplate(getTemplate("footer.vm"));
> 
> or something like that...
> But how should this be solved with a single template?
> 
> Thanks is advance,
> Jonas Bengtsson
> 
> 
> > -----Original Message-----
> > From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
> > Sent: Friday, November 02, 2001 12:52 AM
> > To: velocity-user@jakarta.apache.org
> > Subject: Re: Multiple templates in servlets
> >
> >
> > On 11/1/01 6:45 PM, "Jonas Bengtsson" <jo...@home.se> wrote:
> >
> > > Hi,
> > > <newbie-quiestion-alert>
> > > Is it possible to use several templates in one servlet. For
> > instance if I
> > > have three templates called: header.vm, content.vm and footer.vm. Is it
> > > possible to use all these templates from the servlet, i.e. not
> > with #parse
> > > directives.
> > > </newbie-question-alert>
> >
> > If I understand what you mean - to not use something like a
> > 'frame' template
> > like
> >
> > #parse("header.vm")
> > #parse("content.vm")
> > #parse("footer.vm")
> >
> > Then you can actually render each individually into the output
> > stream.  The
> > included Velocity servlet isn't designed for this - you will have
> > to modify
> > or roll your own, but yes, it's possible.
> >
> > Can I ask why you want to do this?
> >
> > Geir
> >
> > --
> > 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>
> 


__________________________________________________
Do You Yahoo!?
Find a job, post your resume.
http://careers.yahoo.com

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


Re: Multiple templates in servlets

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/2/01 7:46 AM, "Jonas Bengtsson" <jo...@home.se> wrote:

> ok I rest my case. If the increase of template is only 1 I don't have so
> much to argue about :-)
> Furthermore, my approach might have contradicted the MVC approach?
> 

Well, sort of.   Sort of because on one hand, you would have a strict rule
about there being a header, a body and a footer template, so its not as if
the controller was making any decisions about view.  On the other hand, the
designer really is constrained in making layout choices, having no choices
:)

My note was not a criticism as much as an observation - although I didn't
really understand what issues you were working around.  (If it was as simple
as trying to avoid having the layout templates, then I do get it..)

The nice thing about the frameing template approach is that you get the
option to easily mix things up and try new layouts w/o ever having to touch
java code...

I hope this answers the question.

> Thanks for the help!
> /Jonas
> 
>> -----Original Message-----
>> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
>> Sent: Friday, November 02, 2001 1:38 AM
>> To: velocity-user@jakarta.apache.org
>> Subject: Re: Multiple templates in servlets
>> 
>> 
>> On 11/1/01 7:26 PM, "Jonas Bengtsson" <jo...@home.se> wrote:
>> 
>>> Yes you have understood me correctly.
>>> Would it require much rework?
>>> 
>>> One thing would be to reduce the number of files.
>> 
>> Hm.  That strikes me as false economy, somehow, as you have to rewrite the
>> servlet, and do things differently than the ususal.  There is
>> nothing wrong
>> with being different per se, but to save 1 template? :)
>> 
>>> Another problem with a single template is, for instance, when one has a
>>> plug-in page:
>>> * each plug-in will generate some output to the page
>>> * there is no way of knowing which plug-ins there are
>>> If there would be a parseTemplate() method one could do like this:
>>> parseTemplate(getTemplate("header.vm"));
>>> for(int i=0;i<plugIns.length;i++) {
>>>  parseTemplate(plugIns[i].generateTemplate(context));
>>> }
>>> parseTemplate(getTemplate("footer.vm"));
>>> 
>>> or something like that...
>>> But how should this be solved with a single template?
>> 
>> When you say 'plug in' do you mean something like where the body changes
>> based on request, but the rest, header, footer, nav stay the same?
>> 
>> One solution is to use references in the #parse() such as
>> 
>> #parse( $header )
>> #parse( $body )
>> #parse( $footer )
>> 
>> So that the servlet chooses the 'plug-in' as $body, and puts that in the
>> context.  Then when it renders, it uses the body specified.
>> 
>> To match the example above, you could always do...
>> 
>> context.put("header", "header.vm");
>> context.put("plugins", plugins );
>> context.put("footer", "footer.vm");
>> 
>> and then render a 'frame' template that looks like
>> 
>> <table>
>>  <tr><td>#parse($header) </td></tr>
>>  <tr><td>
>>    #foreach( $template in $plugins )
>>      #parse( $template )
>>    #end
>>  </td></tr>
>>  <tr><td>#parse($footer) </td></tr>
>> </table>
>> 
>> This is a bit simplistic, but I think you get the idea...?
>> 
>> Are we on the right track?
>> 
>>> Thanks is advance,
>>> Jonas Bengtsson
>>> 
>>> 
>>>> -----Original Message-----
>>>> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
>>>> Sent: Friday, November 02, 2001 12:52 AM
>>>> To: velocity-user@jakarta.apache.org
>>>> Subject: Re: Multiple templates in servlets
>>>> 
>>>> 
>>>> On 11/1/01 6:45 PM, "Jonas Bengtsson" <jo...@home.se> wrote:
>>>> 
>>>>> Hi,
>>>>> <newbie-quiestion-alert>
>>>>> Is it possible to use several templates in one servlet. For
>>>> instance if I
>>>>> have three templates called: header.vm, content.vm and
>> footer.vm. Is it
>>>>> possible to use all these templates from the servlet, i.e. not
>>>> with #parse
>>>>> directives.
>>>>> </newbie-question-alert>
>>>> 
>>>> If I understand what you mean - to not use something like a
>>>> 'frame' template
>>>> like
>>>> 
>>>> #parse("header.vm")
>>>> #parse("content.vm")
>>>> #parse("footer.vm")
>>>> 
>>>> Then you can actually render each individually into the output
>>>> stream.  The
>>>> included Velocity servlet isn't designed for this - you will have
>>>> to modify
>>>> or roll your own, but yes, it's possible.
>>>> 
>>>> Can I ask why you want to do this?
>>>> 
>>>> Geir
>>>> 
>>>> --
>>>> 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>
>>> 
>> 
>> --
>> Geir Magnusson Jr.     geirm@optonline.net
>> System and Software Consulting
>> "Whoever would overthrow the liberty of a nation must begin by
>> subduing the
>> freeness of speech." - 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>
> 

-- 
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: Multiple templates in servlets

Posted by Jonas Bengtsson <jo...@home.se>.
ok I rest my case. If the increase of template is only 1 I don't have so
much to argue about :-)
Furthermore, my approach might have contradicted the MVC approach?

Thanks for the help!
/Jonas

> -----Original Message-----
> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
> Sent: Friday, November 02, 2001 1:38 AM
> To: velocity-user@jakarta.apache.org
> Subject: Re: Multiple templates in servlets
>
>
> On 11/1/01 7:26 PM, "Jonas Bengtsson" <jo...@home.se> wrote:
>
> > Yes you have understood me correctly.
> > Would it require much rework?
> >
> > One thing would be to reduce the number of files.
>
> Hm.  That strikes me as false economy, somehow, as you have to rewrite the
> servlet, and do things differently than the ususal.  There is
> nothing wrong
> with being different per se, but to save 1 template? :)
>
> > Another problem with a single template is, for instance, when one has a
> > plug-in page:
> > * each plug-in will generate some output to the page
> > * there is no way of knowing which plug-ins there are
> > If there would be a parseTemplate() method one could do like this:
> > parseTemplate(getTemplate("header.vm"));
> > for(int i=0;i<plugIns.length;i++) {
> >  parseTemplate(plugIns[i].generateTemplate(context));
> > }
> > parseTemplate(getTemplate("footer.vm"));
> >
> > or something like that...
> > But how should this be solved with a single template?
>
> When you say 'plug in' do you mean something like where the body changes
> based on request, but the rest, header, footer, nav stay the same?
>
> One solution is to use references in the #parse() such as
>
> #parse( $header )
> #parse( $body )
> #parse( $footer )
>
> So that the servlet chooses the 'plug-in' as $body, and puts that in the
> context.  Then when it renders, it uses the body specified.
>
> To match the example above, you could always do...
>
> context.put("header", "header.vm");
> context.put("plugins", plugins );
> context.put("footer", "footer.vm");
>
> and then render a 'frame' template that looks like
>
> <table>
>  <tr><td>#parse($header) </td></tr>
>  <tr><td>
>    #foreach( $template in $plugins )
>      #parse( $template )
>    #end
>  </td></tr>
>  <tr><td>#parse($footer) </td></tr>
> </table>
>
> This is a bit simplistic, but I think you get the idea...?
>
> Are we on the right track?
>
> > Thanks is advance,
> > Jonas Bengtsson
> >
> >
> >> -----Original Message-----
> >> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
> >> Sent: Friday, November 02, 2001 12:52 AM
> >> To: velocity-user@jakarta.apache.org
> >> Subject: Re: Multiple templates in servlets
> >>
> >>
> >> On 11/1/01 6:45 PM, "Jonas Bengtsson" <jo...@home.se> wrote:
> >>
> >>> Hi,
> >>> <newbie-quiestion-alert>
> >>> Is it possible to use several templates in one servlet. For
> >> instance if I
> >>> have three templates called: header.vm, content.vm and
> footer.vm. Is it
> >>> possible to use all these templates from the servlet, i.e. not
> >> with #parse
> >>> directives.
> >>> </newbie-question-alert>
> >>
> >> If I understand what you mean - to not use something like a
> >> 'frame' template
> >> like
> >>
> >> #parse("header.vm")
> >> #parse("content.vm")
> >> #parse("footer.vm")
> >>
> >> Then you can actually render each individually into the output
> >> stream.  The
> >> included Velocity servlet isn't designed for this - you will have
> >> to modify
> >> or roll your own, but yes, it's possible.
> >>
> >> Can I ask why you want to do this?
> >>
> >> Geir
> >>
> >> --
> >> 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>
> >
>
> --
> Geir Magnusson Jr.     geirm@optonline.net
> System and Software Consulting
> "Whoever would overthrow the liberty of a nation must begin by
> subduing the
> freeness of speech." - 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: Multiple templates in servlets

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/1/01 7:26 PM, "Jonas Bengtsson" <jo...@home.se> wrote:

> Yes you have understood me correctly.
> Would it require much rework?
> 
> One thing would be to reduce the number of files.

Hm.  That strikes me as false economy, somehow, as you have to rewrite the
servlet, and do things differently than the ususal.  There is nothing wrong
with being different per se, but to save 1 template? :)

> Another problem with a single template is, for instance, when one has a
> plug-in page:
> * each plug-in will generate some output to the page
> * there is no way of knowing which plug-ins there are
> If there would be a parseTemplate() method one could do like this:
> parseTemplate(getTemplate("header.vm"));
> for(int i=0;i<plugIns.length;i++) {
>  parseTemplate(plugIns[i].generateTemplate(context));
> }
> parseTemplate(getTemplate("footer.vm"));
> 
> or something like that...
> But how should this be solved with a single template?

When you say 'plug in' do you mean something like where the body changes
based on request, but the rest, header, footer, nav stay the same?

One solution is to use references in the #parse() such as

#parse( $header )
#parse( $body )
#parse( $footer )

So that the servlet chooses the 'plug-in' as $body, and puts that in the
context.  Then when it renders, it uses the body specified.

To match the example above, you could always do...

context.put("header", "header.vm");
context.put("plugins", plugins );
context.put("footer", "footer.vm");

and then render a 'frame' template that looks like

<table>
 <tr><td>#parse($header) </td></tr>
 <tr><td>
   #foreach( $template in $plugins )
     #parse( $template )
   #end
 </td></tr>
 <tr><td>#parse($footer) </td></tr>
</table>

This is a bit simplistic, but I think you get the idea...?

Are we on the right track?

> Thanks is advance,
> Jonas Bengtsson
> 
> 
>> -----Original Message-----
>> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
>> Sent: Friday, November 02, 2001 12:52 AM
>> To: velocity-user@jakarta.apache.org
>> Subject: Re: Multiple templates in servlets
>> 
>> 
>> On 11/1/01 6:45 PM, "Jonas Bengtsson" <jo...@home.se> wrote:
>> 
>>> Hi,
>>> <newbie-quiestion-alert>
>>> Is it possible to use several templates in one servlet. For
>> instance if I
>>> have three templates called: header.vm, content.vm and footer.vm. Is it
>>> possible to use all these templates from the servlet, i.e. not
>> with #parse
>>> directives.
>>> </newbie-question-alert>
>> 
>> If I understand what you mean - to not use something like a
>> 'frame' template
>> like
>> 
>> #parse("header.vm")
>> #parse("content.vm")
>> #parse("footer.vm")
>> 
>> Then you can actually render each individually into the output
>> stream.  The
>> included Velocity servlet isn't designed for this - you will have
>> to modify
>> or roll your own, but yes, it's possible.
>> 
>> Can I ask why you want to do this?
>> 
>> Geir
>> 
>> --
>> 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>
> 

-- 
Geir Magnusson Jr.     geirm@optonline.net
System and Software Consulting
"Whoever would overthrow the liberty of a nation must begin by subduing the
freeness of speech." - Benjamin Franklin



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


RE: Multiple templates in servlets

Posted by Jonas Bengtsson <jo...@home.se>.
Yes you have understood me correctly.
Would it require much rework?

One thing would be to reduce the number of files.
Another problem with a single template is, for instance, when one has a
plug-in page:
* each plug-in will generate some output to the page
* there is no way of knowing which plug-ins there are
If there would be a parseTemplate() method one could do like this:
parseTemplate(getTemplate("header.vm"));
for(int i=0;i<plugIns.length;i++) {
   parseTemplate(plugIns[i].generateTemplate(context));
}
parseTemplate(getTemplate("footer.vm"));

or something like that...
But how should this be solved with a single template?

Thanks is advance,
Jonas Bengtsson


> -----Original Message-----
> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
> Sent: Friday, November 02, 2001 12:52 AM
> To: velocity-user@jakarta.apache.org
> Subject: Re: Multiple templates in servlets
>
>
> On 11/1/01 6:45 PM, "Jonas Bengtsson" <jo...@home.se> wrote:
>
> > Hi,
> > <newbie-quiestion-alert>
> > Is it possible to use several templates in one servlet. For
> instance if I
> > have three templates called: header.vm, content.vm and footer.vm. Is it
> > possible to use all these templates from the servlet, i.e. not
> with #parse
> > directives.
> > </newbie-question-alert>
>
> If I understand what you mean - to not use something like a
> 'frame' template
> like
>
> #parse("header.vm")
> #parse("content.vm")
> #parse("footer.vm")
>
> Then you can actually render each individually into the output
> stream.  The
> included Velocity servlet isn't designed for this - you will have
> to modify
> or roll your own, but yes, it's possible.
>
> Can I ask why you want to do this?
>
> Geir
>
> --
> 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: Multiple templates in servlets

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/1/01 6:45 PM, "Jonas Bengtsson" <jo...@home.se> wrote:

> Hi,
> <newbie-quiestion-alert>
> Is it possible to use several templates in one servlet. For instance if I
> have three templates called: header.vm, content.vm and footer.vm. Is it
> possible to use all these templates from the servlet, i.e. not with #parse
> directives.
> </newbie-question-alert>

If I understand what you mean - to not use something like a 'frame' template
like

#parse("header.vm")
#parse("content.vm")
#parse("footer.vm")

Then you can actually render each individually into the output stream.  The
included Velocity servlet isn't designed for this - you will have to modify
or roll your own, but yes, it's possible.

Can I ask why you want to do this?

Geir

-- 
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>