Class MustacheView
- All Implemented Interfaces:
View
@RestOp-annotated methods to ask the framework to
render a Mustache template.
Companion to MustacheMixin and MustacheViewRenderer: the mixin sets up
the /mustache/* mount and registers the renderer; the renderer detects
MustacheView returns in the response-processor chain and asks the configured
com.github.mustachejava.MustacheFactory to compile(templateName) and then
execute(writer, scope) directly onto the response writer.
Usage:
The template name is factory-relative — the configured
com.github.mustachejava.MustacheFactory resource resolver prepends its own resource
root. With the bridge's default factory + basePath("/templates/"),
MustacheView.of("hello.mustache") resolves to templates/hello.mustache on the
classpath.
Template suffix:
Unlike the Thymeleaf bridge (which always appends .html via the engine resolver's
suffix setting), mustache.java does not have a built-in resolver-suffix concept — the
literal template name is what factory.compile(...) sees. The
templateSuffix(...) builder knob
fills the gap: when set, the bridge appends the suffix to template names that don't already
end with it (idempotent), so the caller can write MustacheView.of("hello") and the
bridge resolves it as "hello.mustache" on the classpath.
Immutability:
Each attr(...) / attrs(...) / header(...) call returns a new
MustacheView carrying the additional binding; the original instance is unchanged. This
keeps MustacheView a value type safe to share across requests (e.g. as a static
singleton built once and returned from many handlers).
See Also:
- Since:
- 10.0.0
-
Method Summary
Modifier and TypeMethodDescriptionReturns a copy of this view with the given attribute added (or replaced if a binding forkeyalready exists).Returns a copy of this view with all entries from the given map added (or replaced if a binding for any key already exists).The attributes to expose to the template.Optional response headers applied by the renderer before forwarding to the templating engine.The name of the template to render — typically a path relative to the mixin-configured base path.Returns a copy of this view with the given response header set on the rendered response.static MustacheViewCreates a newMustacheViewcarrying the given template name and no attributes.toString()
-
Method Details
-
of
Creates a newMustacheViewcarrying the given template name and no attributes.The template name is factory-relative; the configured
com.github.mustachejava.MustacheFactoryresource resolver supplies the prefix (resource root). With the bridge's default factory +basePath("/templates/")andtemplateSuffix(".mustache"),"hello"resolves totemplates/hello.mustache; withouttemplateSuffix, the literal name is used as-is.- Parameters:
templateName- The template name (factory-relative, suffix optional). Must not benullor blank.- Returns:
- A new
MustacheViewinstance. - Throws:
IllegalArgumentException- IftemplateNameisnullor blank.
-
attr
Returns a copy of this view with the given attribute added (or replaced if a binding forkeyalready exists).The full attribute map is passed to mustache.java as the rendering scope, so each entry is accessible inside the template as
{{key}}.nullvalues are rejected at build-time because anullscope binding in mustache.java renders as an empty string, which silently masks what is typically a caller bug; rejecting up-front surfaces the mistake at the call site (matches the Thymeleaf-bridge precedent).- Parameters:
key- The attribute key (exposed inside the template as{{key}}). Must not benullor blank.value- The attribute value. Must not benull.- Returns:
- A new
MustacheViewcarrying the additional attribute. - Throws:
IllegalArgumentException- Ifkeyisnullor blank, orvalueisnull.
-
attrs
Returns a copy of this view with all entries from the given map added (or replaced if a binding for any key already exists).- Parameters:
values- The attributes to add.nullis treated as an empty map (the result is the same instance with no entries added).- Returns:
- A new
MustacheViewcarrying the additional attributes. - Throws:
IllegalArgumentException- If any entry has anull/blank key or anullvalue.
-
header
Returns a copy of this view with the given response header set on the rendered response.Typical usage:
.header("Content-Type", "text/html; charset=UTF-8")or.header("Cache-Control", "no-store"). The renderer applies these headers viaHttpServletResponse.setHeader(...)before writing the rendered output.- Parameters:
name- The header name. Must not benullor blank.value- The header value. Must not benull.- Returns:
- A new
MustacheViewcarrying the additional response header. - Throws:
IllegalArgumentException- Ifnameisnullor blank, orvalueisnull.
-
getTemplateName
Description copied from interface:ViewThe name of the template to render — typically a path relative to the mixin-configured base path.For example,
"hello.jsp"with a JSP mixinbasePath("/WEB-INF/views/")resolves to"/WEB-INF/views/hello.jsp"on the request dispatcher.- Specified by:
getTemplateNamein interfaceView- Returns:
- The template name. Never
null.
-
getAttributes
Description copied from interface:ViewThe attributes to expose to the template.Each entry is forwarded to the templating engine as a named binding accessible from inside the template (e.g.
${name}in JSP / EL,th:text="${name}"in Thymeleaf).- Specified by:
getAttributesin interfaceView- Returns:
- The attributes map. Never
null; returns an immutable empty map when no attributes are bound.
-
getResponseHeaders
Description copied from interface:ViewOptional response headers applied by the renderer before forwarding to the templating engine.Typical use cases: setting
Content-Type(e.g."text/html; charset=UTF-8") orCache-Controlon the rendered response. The default implementation returns an empty map so mostViewimpls don't need to override it.- Specified by:
getResponseHeadersin interfaceView- Returns:
- An immutable map of headers to apply. Never
null; defaults to an empty map.
-
toString
-