[prev in list] [next in list] [prev in thread] [next in thread] 

List:       velocity-user
Subject:    Re: Velocity alternative for WebMacro's Template.getParam?
From:       Stefan Rotman <stefan.rotman () gmail ! com>
Date:       2004-11-23 13:07:17
Message-ID: 5ba4ebf304112305077915a10 () mail ! gmail ! com
[Download RAW message or body]

Our argument for working this way is indeed what you described as the
third argument.

In this case we're talking about a backoffice servlet, and we really
don't want to put all of our db objects in the context, only the one's
needed for the requested templates.

Your hint to set up some lazy instantiator or facade is certainly
worth looking into, however, at the moment we kinda lack time to do
so. It is however something that I will remember, and note as a todo!

Greetz,
Stefan.

On Mon, 15 Nov 2004 15:21:56 +0000, Simon Christian
<simon@stoutstick.com> wrote:
> Pondering this, I'm sure there's been a thread on this topic in the past
> - not specifically the WebMacro feature replacement but putting
> template-specified elements into the context. However I can't find that
> thread...
> 
> Quite simply, and not meaning to diss Claude's smart idea: what's the
> point of doing it at all? Why not just push all available data into the
> context that the template might require. I can only really think of
> three arguments for doing so:
> 
> 1. Validation
> 
> This really shouldn't be done in the template anyway, at least if you're
> following some kind of MVC framework. I think general consensus would
> put this elsewhere.
> 
> 2. Wary of giving 'too much' data to the template designer.
> 
> This is cancelled out immediately by the fact that they would have been
> able to 'request' the data to be put into the context anyway.
> 
> 3. Some objects are costly to create, so it's more efficient to only
> create them when required.
> 
> True, however it's easy to set up some lazy instantiator or facade so
> that the 'expensive' objects aren't created until the template requires
> them.
> 
> I'm sure others have better arguments than I for/against doing this.
> 
> - simon
> 
> 
> 
> 
> Claude Brisson wrote:
> 
> > If I understand correctly, it's a way to ask a template for its needs on
> > the java side, before merging it with a context.
> >
> > Interesting.
> >
> > One way I think of would be to traverse the nodes of the parsed template
> > and make a distinct count of the ASTReferenceNode nodes.
> >
> > Something like :
> >
> > public List getParameters(Template template) {
> >       Node rootNode = template.getData();
> >       List result = new ArrayList();
> >       return getTreeParameters(rootNode,result);
> > }
> >
> > public List getTreeParameters(Node node,List list) {
> >       if (node instanceof ASTReference) {
> >               // we lack a ASTReference.getReferenceType() method
> >               String ref = ((ASTReference)node).getRootString();
> >               // don't take into account "bad" references
> >               if (ref.indexOf('$')!=-1 || ref.indexOf('!')!=-1) {
> >                       return list;
> >               }
> >               if (!list.contains(ref)) {
> >                       list.add(ref);
> >               }
> >               // don't traverse children of a ASTReference,
> >               // since if present, they follow a dot
> >       }
> >       else for (int i=0;i<node.jjtGetNumChildren();i++) {
> >               list = getTreeParameters(node.jjtGetChild(i),list);
> >       }
> >       return list;
> > }
> >
> > (neither compiled or tested...)
> >
> > Claude
> >
> > On Mon, 2004-11-15 at 09:32, Stefan Rotman wrote:
> >
> >>Hi,
> >>
> >>I'm checking out the possibility to migrate some of our WebMacro
> >>projects to Velocity. Now there's one thing that we use a lot in
> >>WebMacro, for which I can't find a Velocity alternative just yet.
> >>
> >>We use the Templage.getParam function from WebMacro to get some
> >>parameters before
> >>parsing the template.
> >>
> >>Javadoc at:
> >>http://www.webmacro.org/api/org/webmacro/Template.html#getParam(java.lang.String)
> >>
> >>Let me explain with a little example:
> >>
> >>We have a WebMacro template (let's call it template.wm) looking like this:
> >>
> >><html>
> >><body>
> >>#param $require = ["foo", "bar"]
> >>
> >>Foo value is $foo
> >>Bar value is $bar
> >></body>
> >></html>
> >>
> >>Now when our servlet is instructed to return the template.wm Template,
> >>the code looks somewhat like this:
> >>
> >>public class FooServlet extends WMServlet {
> >>    public Template handle(WebContext context) {
> >>        Template template = getTemplate("template.wm");
> >>        Object[] requiredParams = (Object[])template.getParam("require");
> >>        for (int i = 0; i < requiredParams.length; i++)
> >>            context.put(requiredParams[i], "-Value for "+requiredParams[i]+"-");
> >>        return template;
> >>    }
> >>}
> >>
> >>This way the values for foo and bar are known when the template is displayed.
> >>
> >>Is there a right way to do this in Velocity? The closest I came to
> >>this using Velocity is creating a new VelocityContext, merge the
> >>Template with this context, and get the value for require from that
> >>context, but when doing this I get warnings in my log files about $foo
> >>and $bar that are not yet set, so I really would like a "cleaner" way
> >>to do this.
> >>
> >>Thanks in advance,
> >>Stefan.
> >>
> >>---------------------------------------------------------------------
> >>To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> >>For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> >>
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic