java.lang.Object
org.klojang.templates.StringifierRegistry
A registry of stringifiers used by the
RenderSession
to stringify the values coming back from the data access
layer. Stringifiers can be used, for example, to apply non-standard formatting to
dates and numbers, or to apply some sort of escaping (e.g.
HTML escaping), or to stringify objects whose
toString()
method does not satisfy your needs. If you need to configure
stringifiers, your code might look something like this:
StringifierRegistry stringifiers = StringifierRegistry.configure() .forType(int.class, obj -> String.valueOf((int) obj + 10)) .freeze(); Template template = Template.fromString("~%foo%"); RenderSession session = template.newRenderSession(stringifiers); String out = session.set("foo", 32).render(); // 42
In practice, you are more likely to create just a single
StringifierRegistry
instance for your entire application, when it starts
up, and pass that instance to all calls to
Template.newRenderSession()
.
This is how a StringifierRegistry
decides which stringifier to use for
a template variable:
- If a stringifier has been registered for a variable group and the variable belongs to that group, then that is the stringifier that is going to be used.
- If a stringifier has been registered for that particular variable in that particular template, then that is the stringifier that is going to be used.
- If a stringifier has been registered for all variables with that particular
name (irrespective of the template they belong to), then that is the
stringifier that is going to be used. See
StringifierRegistry.Builder.forName(String, Stringifier)
- If a stringifier has been registered for the data type of that particular variable, then that is the stringifier that is going to be used.
- If you have registered an alternative default stringifier, then that is the stringifier that is going to be used.
- Otherwise
Stringifier.DEFAULT
is going to be used.
- Author:
- Ayco Holleman
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringifier
To be used for escaping HTML attributes.static final Stringifier
Applies HTML escaping.static final Stringifier
Applies Javascript escaping.static final Stringifier
To be used for escaping HTML attributes containing Javascript, likeonclick
.static final Stringifier
To be used for escaping URL path segments.static final Stringifier
To be used for escaping URL query parameter.static final StringifierRegistry
A minimalStringifierRegistry
instance. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringifierRegistry.Builder
Returns aBuilder
instance that lets you configure aStringifierRegistry
.static StringifierRegistry.Builder
Returns aBuilder
instance that lets you configure aStringifierRegistry
.
-
Field Details
-
STANDARD_STRINGIFIERS
A minimalStringifierRegistry
instance. It contains stringifiers for the predefinedvariable groups
. Variables not within these groups are stringified using the default stringifier. This is theStringifierRegistry
aRenderSession
will use if you calledTemplate.newRenderSession()
without theStringifierRegistry
argument. -
ESCAPE_HTML
Applies HTML escaping. This is one of the standard stringifiers. It is the stringifier used by theHTML
variable group. -
ESCAPE_JS
Applies Javascript escaping. This is one of the standard stringifiers. It is the stringifier used by theJS
variable group. -
ESCAPE_ATTR
To be used for escaping HTML attributes. Same asESCAPE_HTML
except that single quotes and double quotes are also escaped. This is one of the standard stringifiers. It is the stringifier used by theATTR
variable group. -
ESCAPE_JS_ATTR
To be used for escaping HTML attributes containing Javascript, likeonclick
. This is one of the standard stringifiers. It is the stringifier used by theJS_ATTR
variable group. -
ESCAPE_QUERY_PARAM
To be used for escaping URL query parameter. Both parameter names and parameter values can be escaped using this stringifier since they are escaped identically. This is one of the standard stringifiers. It is the stringifier used by thePARAM
variable group. -
ESCAPE_PATH
To be used for escaping URL path segments. This is one of the standard stringifiers. It is the stringifier used by thePATH
variable group.
-
-
Method Details
-
configure
Returns aBuilder
instance that lets you configure aStringifierRegistry
. TheStringifierRegistry
will already contain the default stringifier and the stringifiers for the standard variable groups.- Returns:
- A
Builder
instance that lets you configure aStringifierRegistry
-
cleanSlate
Returns aBuilder
instance that lets you configure aStringifierRegistry
. The registry will not contain any stringifier except the default stringifier. Useful for non-HTML templates.- Returns:
- A
Builder
instance that lets you configure aStringifierRegistry
-