Stringifier
. Within a template,
variables can be assigned to a variable group by using the variable group's name
as a prefix. For example: ~%html:firstName%
or ~%js:firstName%
or
~%myDateFormat:birthDate%
. The first two examples specify the predefined
HTML
and JS
variable groups, which provide HTML-escaping and
ECMASScript-escaping, respectively. The predefined variable groups mostly rely on
apache.commons.text
to do the actual escaping. The third example is a custom group that you can define
using the StringifierRegistry
class. Variable groups can also be assigned
in a more
ad hoc manner using the API. Many methods of the RenderSession
class take a VarGroup
argument.
Note that variable groups are assigned at the
variable occurrence level. For example, a template
may contain multiple instances of a variable named firstName
. For
occurrences inside a <script>
tag you might want to use the js:
prefix, for the others the html:
prefix. Therefore, stringifiers
associated with a variable group take the highest precedence, even higher, perhaps
counterintuitively, than stringifiers associated with a variable.
- Author:
- Ayco Holleman
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final VarGroup
A predefined variable group corresponding to theattr:
prefix.static final VarGroup
A predefined variable group corresponding to thedef:
prefix.static final VarGroup
A predefined variable group corresponding to thehtml:
prefix.static final VarGroup
A predefined variable group corresponding to thejs:
prefix.static final VarGroup
A predefined variable group corresponding to thejsattr:
prefix.static final VarGroup
A predefined variable group corresponding to theparam:
prefix.static final VarGroup
A predefined variable group corresponding to thepath:
prefix.static final VarGroup
A predefined variable group corresponding to thetext:
prefix. -
Method Summary
Modifier and TypeMethodDescriptionstatic VarGroup
Returns theVarGroup
with the specified name (which is also the prefix to be used inside a template).getName()
Returns the name of thisVarGroup
, which is also the prefix to be used inside a template.toString()
Returns the name of thisVarGroup
, which is also the prefix to be used inside a template.
-
Field Details
-
TEXT
A predefined variable group corresponding to thetext:
prefix. Forces the variable instance to be stringified using thedefault stringifier
. -
HTML
A predefined variable group corresponding to thehtml:
prefix. Variables with this prefix are HTML-escaped. Note that the fact alone that a variable appears inside an HTML element, as in<td>~%age%</td>
, does not mean that the variable has to have thehtml:
prefix. Theage
variable likely is an integer, which does not require any HTML escaping. -
JS
A predefined variable group corresponding to thejs:
prefix. Variables with this prefix are ECMASScript-escaped. Especially for use in<script>
tags. -
ATTR
A predefined variable group corresponding to theattr:
prefix. Works just like thehtml:
prefix except that single and double quote characters are also escaped. Especially for use in element attribute values. -
JS_ATTR
A predefined variable group corresponding to thejsattr:
prefix. Variables with this prefix are first JavaScript-escaped and then HTML-escaped like theATTR
variable group. Especially for use in HTML attributes that contain JavaScript, likeonclick
. -
PARAM
A predefined variable group corresponding to theparam:
prefix. To be used for template variables placed inside a URL as the value of a query parameter, likehttp://example.com/?id=~%param:id%
. It could also be used in the more unlikely case that the variable functions as the name of the query parameter, because names and values are escaped identically in a URL. Note that it does not matter whether the URL as a whole is the value of a JavaScript variable or the contents of an HTML tag. Further escaping using either JavaScript-escaping rules or HTML-escaping rules would not change the value. -
PATH
A predefined variable group corresponding to thepath:
prefix. To be used for template variables placed inside a URL as a path segment. For example:http://example.com/~%path:city%/~%path:restaurant%
. -
DEF
A predefined variable group corresponding to the
def:
prefix. This prefix will causenull
values to be replaced with the placeholder value defined within the template. The example below will render as "Good morning, John" iffirstName
wasnull
.Good morning, <!-- ~%def:firstName% -->John<!--%-->
See
Regex.REGEX_CMT_VARIABLE
for an explanation of the syntax and purpose of placeholder values.
-
-
Method Details
-
forName
Returns theVarGroup
with the specified name (which is also the prefix to be used inside a template). Throws anIllegalArgumentException
if there is noVarGroup
with the specified name.- Parameters:
name
- the name or prefix- Returns:
- the
VarGroup
instance corresponding to the specified name
-
getName
Returns the name of thisVarGroup
, which is also the prefix to be used inside a template.- Returns:
- the name of this
VarGroup
-
toString
Returns the name of thisVarGroup
, which is also the prefix to be used inside a template.
-