Differences between revisions 4 and 5
Revision 4 as of 2006-04-18 10:13:44
Size: 2201
Editor: gatekeeper
Revision 5 as of 2009-09-20 22:06:12
Size: 2203
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
Fred Toth's original solution can be found [http://mail-archives.apache.org/mod_mbox/jakarta-velocity-user/200405.mbox/%3c6. here]. Fred Toth's original solution can be found [[http://mail-archives.apache.org/mod_mbox/jakarta-velocity-user/200405.mbox/%3c6.|here]].

Fred Toth came up with a great idea on a "silent" #parse/#include. Unfortunately, it uses the SingletonModel, which means that it does not work with the VelocityViewServlet in VelocityTools 1.2. Here is the same solution written for 1.2.

Fred Toth's original solution can be found here.

  • A mini "tool" for the tool box:

package org.apache.velocity.tools.generic;

import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.tools.view.context.ViewContext;
import org.apache.velocity.tools.view.tools.ViewTool;

public class TemplateExistsTool implements ViewTool
    private VelocityEngine engine = null;
    public VelocityEngine getVelocityEngine()
        return this.engine;

    public void setVelocityEngine(VelocityEngine engine)
        this.engine = engine;

    public void init(Object obj)
        // if ViewContext, try to retrieve a VelocityEngine for use
        if (obj instanceof ViewContext)
            this.setVelocityEngine(((ViewContext) obj).getVelocityEngine());

    public boolean exists(String template)
        if (this.engine == null)
            return Velocity.resourceExists(template);
        return this.engine.templateExists(template);

  • A tool box entry:

  • And a couple of macro definitions:

#macro( cinclude $file )##
#macro( cparse $file )##

With the above you can use these macros to get the job done:


If the files are not there, the macro silently evaluates to the empty string. If the files exist, you get the normal behavior.

SilentParseInclude (last edited 2009-09-20 22:06:12 by localhost)