Renders HTML pages by combining a theme's chrome with a page's own template, then expanding any dynamic components in the merged markup. Registered as the templater variable in the Velocity rendering context for every request, so page templates call it directly to accumulate JS, CSS and LESS dependencies (pushJs, pushCss, pushLess), to combine those into munged bundle URLs, to merge page and template titles, and to read the CSS classes accumulated for the page body. Locates the master and theme templates by walking up from the requested resource, fires a PreRenderEvent before rendering so apps can intervene (for example to disable compiled dependencies), and finally runs the rendered output through DynamicComponentService to expand any dynamic components it contains.
Implements: HtmlTemplater
Properties
| Property | Returns | Description |
|---|---|---|
| allTemplateVars | Map<String,Object> | A snapshot of every variable currently bound in the page render datamodel for the current request, keyed by variable name. Returns an empty map if there is no current request or no render datamodel has been set on it, or if it is not a VelocityContext. |
| bodyClasses | String | The de-duplicated list of body CSS classes for the current page, combining the classes declared on the theme template, the page template and the page resource itself, joined with a space. Computed once per request and cached on the request attributes; returns null if there is no current request or no render datamodel has been set on it yet. |
| cssSources | Map<String,List<String>> | The CSS sources pushed so far during the current request via pushCss, keyed by media type. Unlike getJsSources and getLessSources, this returns the raw request attribute directly: it is null if nothing has been pushed yet, and throws if there is no current request. |
| jsSources | Map<String,List<String>> | The JS sources pushed so far during the current request, keyed by group and ordered by the position each source was pushed with. Returns an empty map if nothing has been pushed or there is no current request. |
| lessSources | Map<String,List<String>> | The LESS sources pushed so far during the current request, keyed by media type and ordered by the position each source was pushed with. Returns an empty map if nothing has been pushed or there is no current request. |
| webRoot | String | The web root path this templater renders relative to. Defaults to "/". |
Methods
writePage(String templatePath, Resource aThis, Map<String,String> params, OutputStream out) · writePage(String theme, String templatePath, Resource aThis, Map<String,String> params, OutputStream out) · getCssPaths(FileResource page) · getWebRoot() · getBodyClasses() · getTemplateResource(String source) · pushLess(String source, String media) · pushLess(String source, String media, Integer position) · getLessSources() · getCombinedPath(List<String> paths) · pushJs(String source) · pushJs(String source, String group) · pushJs(String source, String group, Integer position) · getJsSources() · getJsSources(String group) · getCombinedJsPath(List<String> paths) · getCombinedJsPath(String group) · getCombinedJsPaths(String group) · pushCss(String source, String media) · getCssSources() · getCombinedCssPath(List<String> paths) · writeResourceNotFoundError(String title, String templatePath, Throwable e, OutputStream out) · writeTemplateParseError(String title, String templatePath, GetableResourcePathTemplateHtmlPage r, ParseErrorException e, OutputStream out) · writeTemplateError(String title, String templatePath, String templateBody, String desc, int line, int column, OutputStream out) · writeTemplateMissingError(String templatePath, OutputStream out) · getClassName(Object resource) · pushCss(String source) · mergeTitles(String templateTitle, String pageTitle) · mergeTitles(String templateTitle, String pageTitle, String defaultTile) · getAllTemplateVars()
writePage(String templatePath, Resource aThis, Map<String,String> params, OutputStream out)
Returns: void
Renders a page using the theme resolved for the given resource. Determines whether the resource is public by checking if it implements CommonResource, finds an appropriate theme from the root folder if possible, then delegates to writePage(String, String, Resource, Map, OutputStream) to do the actual rendering.
| Parameter | Description |
|---|---|
templatePath | the path of the page template to render, or "." to use the resource's own href |
aThis | the resource being rendered |
params | request parameters made available to the template |
out | the stream the rendered HTML is written to |
writePage(String theme, String templatePath, Resource aThis, Map<String,String> params, OutputStream out)
Returns: void
Renders a templated page with the given theme and template. The theme controls the "chrome" (menu, header, footer and so on), while the template controls the layout of this specific page (contacts, calendar, file list and so on). Builds the Velocity datamodel, locates the theme and master templates, fires a PreRenderEvent, pushes web dependencies, runs the Velocity templater, then expands dynamic components in the resulting markup before writing it to the output stream.
| Parameter | Description |
|---|---|
theme | the theme name to render with; ignored unless equal to "custom" |
templatePath | the path of the page template to render, or "." to use the resource's own href |
aThis | the resource being rendered |
params | request parameters made available to the template |
out | the stream the rendered HTML is written to |
getCssPaths(FileResource page)
Returns: List<String>
Returns the CSS paths associated with the given page. Not currently implemented: always returns an empty list regardless of the page passed in.
| Parameter | Description |
|---|---|
page | the file resource to find CSS paths for |
getWebRoot()
Returns: String
The web root path this templater renders relative to. Defaults to "/".
getBodyClasses()
Returns: String
The de-duplicated list of body CSS classes for the current page, combining the classes declared on the theme template, the page template and the page resource itself, joined with a space. Computed once per request and cached on the request attributes; returns null if there is no current request or no render datamodel has been set on it yet.
getTemplateResource(String source)
Returns: GetableResourcePathTemplateHtmlPage
Legacy hook for the old Velocity resource-path template lookup. Not implemented on this templater: always returns null regardless of the source path passed in.
| Parameter | Description |
|---|---|
source | the template path to look up |
pushLess(String source, String media)
Returns: String
Records a LESS source to be rendered for the "all" position, delegating to pushLess(String, String, Integer) with a null position. Called directly from page and theme templates.
| Parameter | Description |
|---|---|
source | the path of the LESS resource to push |
media | the CSS media attribute to render the combined stylesheet with |
pushLess(String source, String media, Integer position)
Returns: String
Records a LESS source against a media type and an optional ordering position, for later combination into a single munged stylesheet URL by getCombinedPath(List). Sources are accumulated per media type on the current request's attributes and deduplicated; a source already pushed for the same media is not added again.
| Parameter | Description |
|---|---|
source | the path of the LESS resource to push |
media | the CSS media attribute the source is grouped under |
position | the ordering position for the source within its media group, or null to place it last |
getLessSources()
Returns: Map<String,List<String>>
The LESS sources pushed so far during the current request, keyed by media type and ordered by the position each source was pushed with. Returns an empty map if nothing has been pushed or there is no current request.
getCombinedPath(List<String> paths)
Returns: String
Combines a list of resource paths into a single munged URL that the front end unpacks and compiles as LESS, tagged with a cache-busting version hash for the current root folder. A single path is returned unchanged apart from the appended hash.
| Parameter | Description |
|---|---|
paths | the resource paths to combine |
pushJs(String source)
Returns: String
Records a JS source under the "default" group, delegating to pushJs(String, String).
| Parameter | Description |
|---|---|
source | the path of the JS resource to push |
pushJs(String source, String group)
Returns: String
Records a JS source under the given group, delegating to pushJs(String, String, Integer) with a null position.
| Parameter | Description |
|---|---|
source | the path of the JS resource to push |
group | the group the source is combined and ordered within |
pushJs(String source, String group, Integer position)
Returns: String
Records a JS source against a group and an optional ordering position, for later combination into one or more munged bundle URLs by getCombinedJsPath(String) or getCombinedJsPaths(String). Sources are accumulated per group on the current request's attributes and deduplicated; a source already pushed for the same group is not added again.
| Parameter | Description |
|---|---|
source | the path of the JS resource to push |
group | the group the source is combined and ordered within |
position | the ordering position for the source within its group, or null to place it last |
getJsSources()
Returns: Map<String,List<String>>
The JS sources pushed so far during the current request, keyed by group and ordered by the position each source was pushed with. Returns an empty map if nothing has been pushed or there is no current request.
getJsSources(String group)
Returns: List<String>
The JS sources pushed so far during the current request for a single group, ordered by the position each source was pushed with. Returns an empty list if nothing has been pushed for the group or there is no current request.
| Parameter | Description |
|---|---|
group | the group to look up pushed sources for |
getCombinedJsPath(List<String> paths)
Returns: String
Combines a list of JS resource paths into a single munged bundle URL with a cache-busting version hash appended.
| Parameter | Description |
|---|---|
paths | the JS resource paths to combine |
getCombinedJsPath(String group)
Returns: String
Combines the JS sources pushed for the given group into a single munged bundle URL.
| Parameter | Description |
|---|---|
group | the group whose pushed sources are combined |
getCombinedJsPaths(String group)
Returns: List<String>
Munges the JS sources pushed for the given group together into bundle URLs, but caps each combined path at a maximum length. If combining every source for the group into one path would exceed that length, the sources are split across multiple munged paths instead of one.
| Parameter | Description |
|---|---|
group | the group whose pushed sources are combined |
pushCss(String source, String media)
Returns: String
Records a CSS source to be output under the given media type, for later combination into a single munged stylesheet URL by getCombinedCssPath(List). Sources are accumulated per media type on the current request's attributes and deduplicated; a source already pushed for the same media is not added again. A null media is treated as an empty string group.
| Parameter | Description |
|---|---|
source | the path of the CSS resource to push |
media | the CSS media attribute the source is grouped under, or null |
getCssSources()
Returns: Map<String,List<String>>
The CSS sources pushed so far during the current request via pushCss, keyed by media type. Unlike getJsSources and getLessSources, this returns the raw request attribute directly: it is null if nothing has been pushed yet, and throws if there is no current request.
getCombinedCssPath(List<String> paths)
Returns: String
Combines a list of CSS resource paths into a single munged bundle URL with a cache-busting version hash appended, with the leading slash stripped since templating adds its own.
| Parameter | Description |
|---|---|
paths | the CSS resource paths to combine |
writeResourceNotFoundError(String title, String templatePath, Throwable e, OutputStream out)
Returns: void
Writes a minimal HTML error page reporting that a template resource could not be found, including the exception message and the template path. Used internally when writePage cannot locate the requested template.
| Parameter | Description |
|---|---|
title | the heading to show at the top of the error page |
templatePath | the path of the template that could not be found |
e | the exception describing the failure; its cause is shown in preference to the exception itself if it is a RuntimeException with a cause |
out | the stream the error page is written to |
writeTemplateParseError(String title, String templatePath, GetableResourcePathTemplateHtmlPage r, ParseErrorException e, OutputStream out)
Returns: void
Writes a minimal HTML error page reporting a Velocity template parse failure, showing the invalid syntax and the parse error message, and delegating to writeTemplateError(String, String, String, String, int, int, OutputStream) to render the offending template body with line numbers.
| Parameter | Description |
|---|---|
title | the heading to show at the top of the error page |
templatePath | the path of the template that failed to parse |
r | the template resource whose body is shown in the error page |
e | the parse exception describing the failure |
out | the stream the error page is written to |
writeTemplateError(String title, String templatePath, String templateBody, String desc, int line, int column, OutputStream out)
Returns: void
Writes a minimal HTML error page reporting a template rendering error, showing the template path, the error line and column, a description of the problem, and the raw template body with line numbers for reference.
| Parameter | Description |
|---|---|
title | the heading to show at the top of the error page |
templatePath | the path of the template the error occurred in; if it contains a colon, only the part after it is used as the link target |
templateBody | the raw template source to display with line numbers |
desc | a description of the error to show on the page |
line | the line number the error occurred at |
column | the column number the error occurred at |
out | the stream the error page is written to |
writeTemplateMissingError(String templatePath, OutputStream out)
Returns: void
Writes a plain-text message to the output stream reporting that the given template path could not be found.
| Parameter | Description |
|---|---|
templatePath | the path of the missing template |
out | the stream the message is written to |
getClassName(Object resource)
Returns: String
The simple (unqualified) class name of the given object, with any package prefix stripped.
| Parameter | Description |
|---|---|
resource | the object to name, or null |
pushCss(String source)
Returns: String
Records a CSS source under no specific media type, delegating to pushCss(String, String) with a null media.
| Parameter | Description |
|---|---|
source | the path of the CSS resource to push |
mergeTitles(String templateTitle, String pageTitle)
Returns: String
Substitutes the literal token $title within templateTitle with pageTitle, then plain-text-formats the result. This lets a page template declare a title pattern containing $title while remaining safe to render on component pages.
| Parameter | Description |
|---|---|
templateTitle | the title pattern from the template, which may contain the token $title |
pageTitle | the page's own title to substitute into templateTitle |
mergeTitles(String templateTitle, String pageTitle, String defaultTile)
Returns: String
Substitutes the literal token $title within templateTitle with pageTitle, as per mergeTitles(String, String), falling back to defaultTile if the result is blank.
| Parameter | Description |
|---|---|
templateTitle | the title pattern from the template, which may contain the token $title |
pageTitle | the page's own title to substitute into templateTitle |
defaultTile | the title to use if the merged result is blank |
getAllTemplateVars()
Returns: Map<String,Object>
A snapshot of every variable currently bound in the page render datamodel for the current request, keyed by variable name. Returns an empty map if there is no current request or no render datamodel has been set on it, or if it is not a VelocityContext.