You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Thomas Åhlen <th...@obidobi.net> on 2001/11/23 09:12:35 UTC

Extended functionality

I am having some problems to manage my templates since I need to parse one
template multiple times.

I have abstracted my content into several layers.

ContentType, Layout, Style and Language.
and categorised them in  <static>, <dynamic> content.

So I build my "RuntimeTemplates" by buildning the Layout first then adding
the Style and Language content categorized as <static>. In my servlets I
create the VelocityContext and do the final parsing adding all dynamic
content.

Since I need to parse the same template more than once I need to add \'es to
the #<directives> for the dynamic content so that aren't removed when I
parse the static content. This is becoming a *pain* in the arse :/. I have
made a webinterface where templates can be managed but \\ sign's are
converted to \ automatically by the webbrowsers. So each time i want to edit
a template i have to add a \ sign to all #parse, #for .. and so on.

A better solution would be something like this:

[template]
#if ( $c = 3 )
 wewq
#end

2#foreach ( $a in $as )
        dfdsfsd
2#end

[template] after one parse
 wewg

1#foreach ( $a in $as )
        dfdsfsd
1#end

[template] after two parses
 wewg

        dfdsfsd
        dfdsfsd


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


Re: Extended functionality

Posted by Jon Stevens <jo...@latchkey.com>.
on 11/23/01 12:12 AM, "Thomas Åhlen" <th...@obidobi.net> wrote:

> I am having some problems to manage my templates since I need to parse one
> template multiple times.

Why not use velocimacro's?

-jon


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


Follow up: Extended functionality

Posted by Thomas Åhlen <th...@obidobi.net>.
> >> I am having some problems to manage my templates since I need to parse
> one
> >> template multiple times.
> >
> >Yuck!  :)

I was previously coding my serverside applications in Perl but have since a
year back converted to Java and Servlet programming. My multi pass parsing
in my perl apps was very simply solved with the CPAN package Template
Toolkit.

In my eyes Template Toolkit is one of the best Template engines out there.
To bad that there only is a perl version, I have actually considered makeing
a java version. Today almost all my needs are covered by Velocity :) so I
rather see Velocity mature to a more powerfull engine than trying to
implement Template Toolkit in java.

Homepage of the perl Template Toolkit(TT) with excellent documentation
http://www.template-toolkit.org/

A must read for all template developers ;). You might get inspired by some
funtionallity.

You might ask how TT solved my multi pass parsing?
In TT all directives have a start and a stop delimiter, called tag style. So
when parsing static content I could tell it to use [* ... *] tags and [% ...
%] in the dynamic parse. So its kind of like having 2 different parsers. As
I see it the possibility for different tag styles is very powerfull and
useful.

How could Velocity solve this and still be able to keep current style?
Add the possibility to have multiple grammar classes and add a property:
runtime.grammar.class = org.apache.velocity.runtime.grammar.Grammar
If you are able to implement the possibility to use multiple grammars in
Velocity I am more than willing to implement a Template Toolkit kind of
grammar.

Thomas


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


Re: Extended functionality

Posted by Thomas Åhlen <th...@obidobi.net>.
>> I am having some problems to manage my templates since I need to parse
one
>> template multiple times.
>
>Yuck!  :)
>
>Reading your next post also, I think I understand what you are trying to
do,
>and wonder if you can take another approach.
>
>Since you can construct templates in pieces, would it make more sense to do
>things like
>
>1) choose a layout template as the main template, with things like
extensive
>use of #parse() and references for pieces of the 'stuff' in the template
>
>2) Use something like CSS for you styling.
>
>3) and ContentType and Language also being supported through resource
>objects (strings...) that you place in the context?

Yes I am doing all of the things you point out! :)

In database I got four template table types
LayoutTemplate, ComponentTemplate, ActionTemplate and RunetimeTemplate
(My webapplication system is composed of multiple servlets. One servlet for
each module, one module can have mutiple actions. Each action can have
multiple States.)

Servlet(module)->action->state
Auth->Login->init
Auth->Login->success

Each chain that are supposed to be display have one template for each
contentType.

[LayoutTemplate]
int Id
int ContentTypeId
string name
string body

[ComponentTemplate]
int Id
int ContentTypeId
string name
string body

[ActionTemplate]
int Id
int ContentTypeId
string name
string body

[RuntimeTemplate]
int Id
int ContentTypeId
int LayoutId
int StyleId
int LanguageId
string name
string body

I build my template this way:
Use an own ResourceLoader called TemplateLoader and the two DataObject
classes representing LayoutTemplate and ComponentTemplate as path's
file.resource.loader.class =
com.obidobi.obiwebapp.wrapper.velocity.TemplateLoader
file.resource.loader.path = com.obidobi.obiwebapp.dob.LayoutTemplate,
com.obidobi.obiwebapp.dob.ActionTemplate
file.resource.loader.cache = false
file.resource.loader.modificationCheckInterval = 0

I have my own cache system build into the DataObject(each table in my
database is a separate DataObject) retreaval system.

This is how the templates are prebuilt as one and stored in Template, pseudo
code:

public static buildTemplate( Vector actionTemplates )
{
    contentType=actionTemplate.getContentType();
    foreach(  actionTemplate )
        foreach( layout )
            load the configuration for this layout and add to velcontext.
            foreach( language )
                load all resourcebundles for this language and add to
velcontext
                foreach( style )
                    load the imageUrl, cssUrl and imageBundle variables and
add to context
                    content = new VelocityContext()
                    parse.evaluate( context, writer, "",
#parse( \"base1.html\")" )
                    save newly paserd template in the [RuntimeTemplate]
table
}

So all kindof static content are added preRuntime and a RuntimeTemplate are
built.
This is how a standard template chain looks like

LayoutTemplate(base1.html)
|->LayoutTemplate(table13.html)
    | | ---> multiple ComponentTemplates
    |-----> one ActionTemplate
               |---> multiple ComponentTemplates

so I do make extensive use of #parse directives and yes I use StyleSheets
for my html style. But as i see it different styles requires different
images, so the imageBundle and resourceUrl parameters handles that.

As you see my final product is a RuntimeTime template which only misses the
dynamic content and the result is that this template will have been parsed
multiple times when ready for display to client.

Therefor my need for 2#parse( "name" ) directives to handle this in a nice
way.

Thomas


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


Re: Extended functionality

Posted by "Geir Magnusson Jr." <ge...@earthlink.net>.
On 11/23/01 3:12 AM, "Thomas Åhlen" <th...@obidobi.net> wrote:

> I am having some problems to manage my templates since I need to parse one
> template multiple times.

Yuck!  :)

Reading your next post also, I think I understand what you are trying to do,
and wonder if you can take another approach.

Since you can construct templates in pieces, would it make more sense to do
things like

1) choose a layout template as the main template, with things like extensive
use of #parse() and references for pieces of the 'stuff' in the template

2) Use something like CSS for you styling.

3) and ContentType and Language also being supported through resource
objects (strings...) that you place in the context?


> 
> I have abstracted my content into several layers.
> 
> ContentType, Layout, Style and Language.
> and categorised them in  <static>, <dynamic> content.
> 
> So I build my "RuntimeTemplates" by buildning the Layout first then adding
> the Style and Language content categorized as <static>. In my servlets I
> create the VelocityContext and do the final parsing adding all dynamic
> content.
> 
> Since I need to parse the same template more than once I need to add \'es to
> the #<directives> for the dynamic content so that aren't removed when I
> parse the static content. This is becoming a *pain* in the arse :/. I have
> made a webinterface where templates can be managed but \\ sign's are
> converted to \ automatically by the webbrowsers. So each time i want to edit
> a template i have to add a \ sign to all #parse, #for .. and so on.
> 
> A better solution would be something like this:
> 
> [template]
> #if ( $c = 3 )
> wewq
> #end
> 
> 2#foreach ( $a in $as )
>       dfdsfsd
> 2#end
> 
> [template] after one parse
> wewg
> 
> 1#foreach ( $a in $as )
>       dfdsfsd
> 1#end
> 
> [template] after two parses
> wewg
> 
>       dfdsfsd
>       dfdsfsd
> 
> 
> --
> 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>