velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christian Reichlin <>
Subject Re: instantiating templates from Strings and absolute paths
Date Fri, 22 Jul 2005 08:43:08 GMT
i had the same problem and i solved it by caching the SimpleNodes which 
are produced by the parse method. i didn't profile my application so i 
don't know if it really works well and it might be a quite stupid way, i 
hadn't time to look at it properly.

private Map<String, SimpleNode> rootNodesCache = new HashMap<String, 
public SimpleNode getRootNodeFor(String template, RuntimeInstance 
runtimeInstance) {

        SimpleNode sn = rootNodesCache.get(template);

        if(sn == null) {
            try {
                Reader reader = new StringReader(template);
                sn = runtimeInstance.parse(reader, tag);

                rootNodesCache.put( template, sn);
            } catch(ParseException e) {
                logger.error("VelocityException for template '" + value 
+ "' :" + e.getMessage());
        return sn;

Xavier Noria wrote:

> I am writing an application that needs a couple of functionalities I  
> don't see in the developer's manual.
> On the one hand I have snippets of templates in a XML file. I've read  
> Velocity.evaluate() can instantiate them, but I would prefer to have  
> a "precompiled" Template object (I'm new to Velocity and I am in fact  
> assuming an object of type Template is a precompiled template). Is  
> that possible or should I need to write my own StringReso
> On the other hand, looks like there is no direct way to create  
> Template objects given an absolute path, except by computing its  
> dirname, setting it as template root dir, then load the basename as  
> relative path. I've seen another workaround which consists of setting  
> the root dir to the empty string. Isn't there a more convinient way  
> like a simple method call?
> -- fxn
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message