Template Engine

Getting started with Compozitor Template Engine

How to use Compozitor Template Engine?

Add to your maven project the dependency below.

<dependency>
    <groupId>org.archmix</groupId>
    <artifactId>compozitor-template</artifactId>
    <version>${compozitorVersion}</version>
</dependency>

Getting started with Compozitor Template Engine

Compozitor Template Engine is built on top of Apache Velocity and you can find the following APIs:

  • Directives

  • Macros

  • Resource Loaders

  • Template Builder

  • Template Context

  • Template Engine Builder

Directives

It is possible to define two kinds of Directives: Line and Block ones. To get a better understanding of what directives are, please read this documentation.

Compozitor gives you a set of Line directives, that you can use in your templates. There is no need to register them, because it is already. Follow them in the table below:

Directive

Template function

#capitalize(${Variable})

#lowercase(${Variable})

#render(directive:capitalize, ${Variable})

#trimall(${Variable})

#uncapitalize(${Variable})

#uppercase(${Variable})

Creating your own Directives

If you want to create your own directives, you can extend the existing implementations for line and block directives. Find them in the table below:

When you create your own directive implementation, you need to register them to the Template Engine.

Macros

It is possible to define Macros. To get a better understanding of what macros are, please read this documentation. In a nutshell, it is a way to reuse templates logics as if they are functions.

When you create macros, you need to register them to the Template Engine.

Template Loaders

Compozitor gives you some kinds of most common template loaders needed. You will be able to load templates from Java Classpath, File System Path, and Amazon S3.

Creating your own template loader

If you want to create your own template loader, you just need to implement velocity class org.apache.velocity.runtime.resource.loader.ResourceLoader

When you create a custom template loader, you need to register it to the Template Engine.

Template Engine

Template Engine itself is composed of some components: Template Engine, Template, and Template Context Data.

Template Engine is the place that you store your parsed Templates, Template Loaders, Directives, and Macros. Template is the place where you apply your Template Context Data to the actual process of getting things replaced to get the output from Template.

To get the concepts clearer, let's follow up with an example. Imagine you want to generate a Java source code based on a table that you have in your database.

The Template has the structure of your target Java source code. Template Context Data is all the information you have about your tables and the Template Engine is where you store all your templates, directives, and macros for template processing.

One common use case for Template Engine is the Code Generation. It is an extension for Template Engine.

Last updated

Was this helpful?