Skip to content

Martini Velocity Functions

The Velocity class in Martini provides a set of functions for evaluating Velocity templates. These functions facilitate the integration of the Apache Velocity engine, a Java-based template engine, with Martini services and Groovy code. The primary advantage of using these functions is the simplification and efficiency they bring to the template parsing process.

Using Velocity Functions in Martini

To utilize Velocity functions within your Martini services:

  1. Access the Velocity functions through the functions node in the Martini Navigator.
  2. In the velocity folder, you'll find a collection of functions designed for parsing Velocity templates.
  3. These functions can be incorporated into your services either by drag-and-drop or through content assist by typing “velocity.” It's important to choose the functions with the appropriate naming convention as per your workspace settings.

Example Implementations

Groovy Script Example

1
2
3
4
5
// Context map creation with variables for template substitution
def context = ['firstName': 'Jon', 'lastName': 'Snow']

// Parsing the template using the context
def parsedTemplate = Velocity.parseTemplate("yourTemplateStringHere", context)

In this Groovy script example, the parseTemplate method from the Velocity class is used. The method takes a template string and a context map as parameters. The context map (context) contains the variables that will be substituted into the template during parsing.

General Application in Martini Services

When applying Velocity functions in Martini services, the process typically involves:

  1. Preparing the Velocity template with placeholders for dynamic content.
  2. Creating a context map or data model with key-value pairs corresponding to the placeholders in the template.
  3. Using a Velocity function to parse the template with the provided context or data model, resulting in a dynamically generated output based on the input values.

This approach is applicable in various scenarios, such as generating customized responses, formatting data, or creating dynamic content for emails, web pages, or other text-based formats.