Class TemplateResolver
A template resolver is a locked-down VarResolver: it registers no system vars, permits only the functions in
SafeFunctions, and reads data only from the map passed to each render. The built-in functions give lists, tables
and per-row projections; see the package documentation for the grammar and the encoding rule.
Inside a projection over a list of maps,
Example:
Notes:
- Inside a projection (
.![...] ,.?[...] ) row fields are bare identifiers. A${id} there reads the render scope, not the row. - Functions added with
TemplateResolver.Builder.functions(VarFunction...)are subject to the allowlist; add them withTemplateResolver.Builder.allowFunctions(String...). - Instances are immutable and thread-safe.
ParsedTemplateinstances are too.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final TemplateResolverA resolver with the default configuration: lenient, no destination. -
Method Summary
Modifier and TypeMethodDescriptioncheck(String template, TemplateCatalog catalog) Checks a template against a catalog without rendering it.static TemplateResolver.Buildercreate()Creates a builder.Parses a template.render(ParsedTemplate template, Map<String, ?> scope) Renders a template using the default value encoder of the configured destination (none if there is no destination).render(ParsedTemplate template, Map<String, ?> scope, UnaryOperator<String> valueEncoder) Renders a template.validate(String template, TemplateCatalog catalog) Likecheck(String, TemplateCatalog)but throws.
-
Field Details
-
DEFAULT
A resolver with the default configuration: lenient, no destination.It does no encoding and renders structures with
Structure.toString(). Resolved values reach the output unescaped, so it is not for HTML, Markdown, JSON or URLs; build a resolver with aDestinationspreset (or an explicit encoder) for those.
-
-
Method Details
-
create
Creates a builder.- Returns:
- A new builder.
-
parse
Parses a template.- Parameters:
template- The template text. May benull .- Returns:
- The parsed template. Never
null . - Throws:
ExprException- In strict mode, if the template is malformed or uses an unknown or denied function. In lenient mode nothing is thrown.
-
render
Renders a template using the default value encoder of the configured destination (none if there is no destination).- Parameters:
template- The template. Must not benull , and must have been parsed by this resolver.scope- The names the template can read. May benull (no names).- Returns:
- The rendered text.
- Throws:
IllegalArgumentException- If the template was parsed by a different resolver.
-
render
public String render(ParsedTemplate template, Map<String, ?> scope, UnaryOperator<String> valueEncoder) Renders a template.- Parameters:
template- The template. Must not benull , and must have been parsed by this resolver.scope- The names the template can read. May benull (no names).valueEncoder- The value encoder. Overrides the destination's encoder;null uses the destination's.- Returns:
- The rendered text.
-
check
Checks a template against a catalog without rendering it. Never throws.Reports, in source order: an identifier not in the catalog, a row field not in its list's catalog, a row field read through a name that is not a declared list, an unknown or denied function (unless the catalog allows them), an unknown var, and a marker that does not compile. The key of a
${name} or${name:default} reference is checked like an identifier, so${secret} is a problem whensecret is not declared; a key that is itself computed (${${x}} ) is read as written and is reported as unknown. The default text after: is not checked.- Parameters:
template- The template text. May benull .catalog- The catalog. Must not benull .- Returns:
- The problems. Empty if the template is clean.
-
validate
Likecheck(String, TemplateCatalog)but throws.- Parameters:
template- The template text. May benull .catalog- The catalog. Must not benull .- Returns:
- The parsed template, if it is clean.
- Throws:
TemplateValidationException- If there are problems.
-