You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Stefan Rotman <st...@gmail.com> on 2004/11/15 09:32:46 UTC

Velocity alternative for WebMacro's Template.getParam?

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


Re: Velocity alternative for WebMacro's Template.getParam?

Posted by Stefan Rotman <st...@gmail.com>.
On Mon, 15 Nov 2004 11:59:53 +0100, Claude Brisson <cl...@renegat.net> 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.

Indeed that's what was intended.

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

I did compile and test this, (with a few modifications ;) ) but it
seems to return just the names of the the parameters that where
declared in the template, not their values.
What I was really looking for was the value of $require, so I'm
affraid this did not quite wat I hoped for.

I guess our migration from WebMacro to Velocity will involve more work
if we have to start looking for an alternative to handle this.

Thanks anyway!
> 
> 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


Re: Velocity alternative for WebMacro's Template.getParam?

Posted by Stefan Rotman <st...@gmail.com>.
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
<si...@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


Re: Velocity alternative for WebMacro's Template.getParam?

Posted by Simon Christian <si...@stoutstick.com>.
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


Re: Velocity alternative for WebMacro's Template.getParam?

Posted by Claude Brisson <cl...@renegat.net>.
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


Re: Velocity alternative for WebMacro's Template.getParam?

Posted by Simon Christian <si...@stoutstick.com>.
Hi Stefan,

I don't think there is a direct alternative to the Templage.getParam 
function, however I guess it rather depends what you want to to with the 
parameters i.e. are you intending to validate the input and if so to 
what level, or do you just need access to them?

If you need to do 'proper' validation, a common solution (though it 
obviously has  much more widespread benefits/effects) is to use Velocity 
with Struts and use the latters' form validation which is defined on a 
per-action basis in XML.

Alternatively if you just want to check that the parameters exist you 
could add a header to each template which uses a tool (some POJO) to 
check the parameters and perform some exit strategy it all's not well.

If you need access to the parameters and want to verify their format 
take a look at the ParameterParser from the VelocityTools project:

   http://jakarta.apache.org/velocity/tools/view/ParameterParser.html

You don't need to use VelocityTools with it - just construct an instance 
and drop it into the request, then you can access the parameters like 
$parameters.getString( "foo" ), $parameters.getBoolean ( "bar" ) etc.

Or of course use similar to populate the context with all the available 
parameters and values. the VelocityViewServlet/VelocityLayoutServlet 
will do this for you if that's an option.

hth,

- simon


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