velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Claude Brisson <cla...@renegat.net>
Subject Re: Velocity alternative for WebMacro's Template.getParam?
Date Mon, 15 Nov 2004 13:34:56 GMT
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


Mime
View raw message