Template Engine

Template Engine is a simple component that is responsible for loading and evaluating templates - abstract pieces of data, that may be converted to String instances. Template is identified with a key (String). TemplateEngine loads templates in 'load' method. TemplateEngine evaluates templates (converts them to String) in 'evaluate' method. Parameters for an evaluating process may be passed as a Map instance to 'evaluate' method - using of this Map is TemplateEngine-dependent.

Velocity Template Engine

aislib comes with implementation of Template Engine based on Jakarta Velocity templates.

VelocityTemplateEngine class may be instantiated in two ways:

  • directly, using constructor:
    TemplateEngine engine = new VelocityTemplateEngine(servletContext, "/WEB-INF/templates");
         
    creates template engine, that loads templates from /WEB-INF/templates directory using ServletContext.getResourceAsStream(String) method.
  • using VelocityConfigHelper class for preparing Velocity configuration. There's a possibilty to configure servlet context loader, Velocity logging based on supplied category (or disable Velocity logging at all) and some other options.
    Map velocityConfig = new HashMap();
    
    // templates will be loaded using servlet context,
    // starting with /WEB-INF/templates directory
    VelocityConfigHelper.configureServletContextLoader(
      velocityConfig, servletContext, "/WEB-INF/templates/");
    
    // Velocity will use application-name.template Commons Logging log instance
    VelocityConfigHelper.configureLog(velocityConfig, "application-name.template");
    
    TemplateEngine engine = new VelocityTemplateEngine(velocityConfig));
          
    There's also a possibility to add URLResourceLoader which loads templates from URLs (so file:// or http:// protocols may be used).