Print
How-to Integrate Templates

Integrating templates into MVEL is easy through the use of org.mvel.TemplateInterpreter class.

Example Usage

The following example uses a Map to provide variables.

Map map = new Map();
map.put("name", "John Doe");
map.put("age", 16);

String myTemplate = "Name: @{name}, Age: @{age}";

String result = TemplateInterpreter.evalToString(myTemplate, map);

assert "Name: John Doe, Age: 16".equals(result);

This example uses a Context object

Person person = new Person();
person.setFavoriteFood("French Fries");

String myTemplate = "My favorite food is: @{favoriteFood}";

String result = TemplateIterpreter.evalToString(myTemplate, person);

assert "My favorite food is: French Fries".equals(result);

There is even a parseToStream method to easily serve template files out to servlets, etc.

Powered by Atlassian Confluence