You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Tim Colson <tc...@cisco.com> on 2002/04/21 22:25:18 UTC

[VELTOOLS] Working, but need advice

Hey folks... I've never claimed to truly know what I'm doing, but I try
hard. <grin>

I've got the VelocityViewServlet running inside my Struts App...and it
actually spits out results from a jsp that used to use the taglib
vel:velocity - which, btw, is very cool to see on its own. :-)

> > ...my biggest issue will still be to handle "page layout" 
> > ...extending VelocityViewServlet to perform a two-pass render...
> > once on the specified template file...and then a second pass 
> > to shove the results of that file into the specified 
> > "layout.vm" file. (for printable, standard, no-left-nav,  etc.)

So I'm looking to this issue next. Because, although I'm getting output,
it lacks the menubar, and other page layout niceties that are a
must-have.

Gabriel wrote: 
> ... did some experiments with connecting VelocityViewServlet with
DVSL. 
> I also used a two-pass rendering process. It worked like this:
> - First, use DVSL to generate Velocity template from XML templates and
>    style sheet. Write the generated Velocity template to disk 
> for later reuse.
> - Secondly, process the Velocity template and merge with app data.

Sounds interesting, but I'm not sure it'll work for my stuff.
Do I understand correctly, the DVSL transform would happen as a batch to
wrap naked "content body only" Velocity templates inside a layout? 

If so, one problem I'd have is that the included left-nav part will vary
at runtime. (i.e. Different users roles will get different left nav
types... so the "layout" needs to include different left-nav velocity
files, or none at all perhaps. 

Also - if there were 5 different layout types, the final # of templates
would be 5X, no?

I think I'm better off with a bit more run-time processing (I think I am
ok to sacrifice the speed loss with this extra runtime overhead.)

After browsing VelocityViewServlet and VelocityServlet... I am trying to
figure out how/where to insert the 'second pass rendering'...
mergeTemplate() is a likely suspect for this, but...

*  merges the template with the context.  
*  Only override this if you really, really
*  really need to. (And don't call us with questions if it breaks :)

hmmm... If it was only "really, really" wouldn't have bothered me, but
that 3rd "really" worries me. ;-)

Suggestions? Perhaps a step by step pseudo-plan? (Especially if it
doesn't involve mergeTemplate()... as I'm a little stumped.)

Recap:
 1) render mypage_content.vm
 2) inside mypage_content.vm, vars are set for use in the "layout"
    #set ($TITLE = "${msg.TITLE} ${msg.get($this_page_sub_title)" )
 3) a layout is selected/inserted into the context by the Controller
(ex. $PAGE_LAYOUT)
 4) mypage_content.vm output is added to the context as $CONTENT
 5) select layout.vm template is loaded and rendered using the same
context

I'm also going to dig back through my emails, as other folks mentioned
some potential ideas. 

Thanks folks,
Tim


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


Velosurf - a db tool for velocity

Posted by Claude <cl...@savoirweb.com>.
Hi folks...

I'd like to share a Velocity db tool that I wrote.

The tarball is for now (temporarily ?) hosted at sourceforge, but I would of course prefer to see it hosted at jakarta.
Although there are some code homogeneization to be achieved here and there before thinking of it (see TODO.txt), I'd be glad to see
it admitted for inclusion in the veltools subproject in a near future.

http://velosurf.sourceforge.net/velosurf-0.2.tgz (~172k)

Please let me now of your questions, remarks...

CloD

----- Here is an extract of the README file :

WHAT IS IT ?

Velosurf is a - thin but efficient - database abstraction layer meant to be use
with Velocity (http://jakarta.apache.org/velocity/index.html), written in Java.
Current version is 0.2 : still quite early in the development process, still some
imperfections, but the package is already functionnal.

The aim of Velosurf is to provide template writers a set of context variables that
automatically map database tables and relationships.

WHY VELOSURF ?

Mainly, to avoid rewriting each time specific database mapping layers in each project involving
velocity and database entities that template writers have to deal with.

----- And here is some sample VTL code using velosurf :

## "Out of the box" usage :

Workers :
#foreach ($worker in $db.worker)
    Name : $worker.firstname $worker.lastname
        position : $db.position.fetch([$worker.position_id]).name
        boss : $db.worker.fetch([$worker.boss_id]).lastname
#end

## when defining foreign keys in a .def file as follow :
##    worker.boss = worker(boss_id)
##    worker.position = position(position_id)
## or directly in the template as follow :

#set ($db.worker.boss = "worker(boss_id)")
#set ($db.worker.position = "position(position_id)")

## the preceding example can be rewritten as :

Workers :
#foreach ($worker in $db.worker)
    Name : $worker.firstname $worker.lastname
        position : $worker.position.name
        boss : $worker.boss.lastname
#end




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


RE: [VELTOOLS] Working, but need advice

Posted by Tim Colson <tc...@cisco.com>.
I took the plunge... and now have a crude but functional two-pass
render. :-)

Code is below, would still greatly appreciate insight, criticism,
suggestions.

BTW - Gabriel brought part of this thread into the DEV list... but I
feel I might be overstepping and out of place here, so I will gladly
take this back over into velocity-user if anybody thinks this topic
isn't appropriate here. 

Thanks folks,
Tim

package Foo;

import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.exception.*;
import org.apache.velocity.tools.view.servlet.VelocityViewServlet;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;

/**
 * This class is mapped in web.xml to handle all *.vm files.
 */

public class LayoutVelocityViewServlet extends VelocityViewServlet {

/**
 * Despite dire warnings, mergeTemplate was overridden with an optional
 * second-pass render if a "LAYOUT" value exists in the context.
 */
    protected void mergeTemplate(Template template, Context context,
HttpServletResponse response)
            throws ResourceNotFoundException, ParseErrorException,
            MethodInvocationException, IOException,
UnsupportedEncodingException, Exception {

        // Check for a layout
        String layoutFile = (String) context.get("LAYOUT");

        if (layoutFile != null) {
            // Load up the layout template
            Template layoutTemplate = null;

            try {
                layoutTemplate = getTemplate(layoutFile);
            } catch (ResourceNotFoundException rnfe) {
                // couldn't find the template
                System.out.println("rnfe = " + rnfe);
            } catch (ParseErrorException pee) {
                // syntax error : problem parsing the template
                System.out.println("pee = " + pee);
            } catch (MethodInvocationException mie) {
                // something invoked in the template threw an exception
                System.out.println("mie = " + mie);
            } catch (Exception e) {
                System.out.println("e = " + e);
            }

            // Render the first pass, i.e. body content
            StringWriter sw = new StringWriter();
            template.merge(context, sw);
            // Add the resulting content to the context
            context.put("CONTENT", sw.toString());
            // Render the second pass, i.e. layout
            super.mergeTemplate(layoutTemplate, context, response);
        } else {
            // no layout specified, so just send the template
            // specified directly to the user.
            super.mergeTemplate(template, context, response);
        }

    }
}


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