Using MVEL templates is straight-forward and easy. Like regular MVEL expressions, they can be executed interpretively, or be pre-compiled and be re-used for faster evaluation.
The org.mvel.templates.TemplateRuntime Class
The TemplateRuntime class is the center of the template engine. You can pass a template to be evaluated to the template engine by way of the eval() method.
In general, the template engine follows all the same rules for context and variable binding, with an overloaded set of eval() methods.
Here's a simple example of parsing a template interpretively:
At the end of execution, the "output" variable will contain the string:
The org.mvel.templates.TemplateCompiler Class
The TemplateCompiler class allows for pre-compilation of the templates.
When you compile a template, a compact, reusable evaluation tree is produced that can be quickly used to evaluate a template. It is used straightforwardly:
At the end of execution, the "output" variable will contain the string: