Class StrTemplate

java.lang.Object
com.peterphi.std.guice.common.ognl.StrTemplate

public final class StrTemplate extends Object
Implements a String Templater that processes strings containing ${...} expressions, and replaces them using a StrLookup instance. To allow templates to contain } characters, the template must start and end with a balanced number of curly braces.

A literal ${...} can be inserted using $${...}

A template may contain } characters; to do this, it is necessary to double-quote as follows:
${{template containing } here }}

For a template to contain }}, it is necessary for the outer template to be expressed as ${{{...}}} and so on. A template's value may not start with { or end with }, since this would be treated as part of the prefix or suffix (respectively).

By default, the result of a template will be recursively evaluated if it contains ${. This can be disabled by using evaluate(String, StrLookup, boolean) with recursive=false, or by the OGNL template using Evaluation Customisation flags. This involves prefixing the OGNL with one or more of the following:

  • :literal: - this will prevent recursive evaluation of the result of this template
  • :html: - this apply HTML escaping to the result of this template (N.B. if :literal: is not specified first, then escaping will be applied to the result of the fully recursive evaluation
  • :xml: - this apply HTML escaping to the result of this template (N.B. if :literal: is not specified first, then escaping will be applied to the result of the fully recursive evaluation
  • :url:path - this apply URL Path escaping to the result of this template (N.B. if :literal: is not specified first, then escaping will be applied to the result of the fully recursive evaluation
  • :url:param - this apply URL Parameter escaping to the result of this template (N.B. if :literal: is not specified first, then escaping will be applied to the result of the fully recursive evaluation

Example of using an Evaluation Customisation flag:

  • ${:literal:url:path:someOgnlStatement()}}
  • ${:html:someOgnlStatementWhoseResultWillBeRecursivelyEvaluated()}}
  • ${:literal:html:someOgnlStatementWhoseResultWillNotBeRecursivelyEvaluatedAndThenEscaped()}}

  • Field Details

  • Method Details

    • evaluate

      public static String evaluate(String template, org.apache.commons.lang.text.StrLookup lookup)
    • evaluate

      public static String evaluate(String template, org.apache.commons.lang.text.StrLookup lookup, boolean recursive)