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

PropertyReturnsDescription
allTemplateVarsMap<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.
bodyClassesStringThe 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.
cssSourcesMap<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.
jsSourcesMap<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.
lessSourcesMap<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.
webRootStringThe 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.

ParameterDescription
templatePaththe path of the page template to render, or "." to use the resource's own href
aThisthe resource being rendered
paramsrequest parameters made available to the template
outthe 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.

ParameterDescription
themethe theme name to render with; ignored unless equal to "custom"
templatePaththe path of the page template to render, or "." to use the resource's own href
aThisthe resource being rendered
paramsrequest parameters made available to the template
outthe 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.

ParameterDescription
pagethe 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.

ParameterDescription
sourcethe 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.

ParameterDescription
sourcethe path of the LESS resource to push
mediathe 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.

ParameterDescription
sourcethe path of the LESS resource to push
mediathe CSS media attribute the source is grouped under
positionthe 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.

ParameterDescription
pathsthe resource paths to combine

pushJs(String source)

Returns: String

Records a JS source under the "default" group, delegating to pushJs(String, String).

ParameterDescription
sourcethe 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.

ParameterDescription
sourcethe path of the JS resource to push
groupthe 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.

ParameterDescription
sourcethe path of the JS resource to push
groupthe group the source is combined and ordered within
positionthe 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.

ParameterDescription
groupthe 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.

ParameterDescription
pathsthe 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.

ParameterDescription
groupthe 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.

ParameterDescription
groupthe 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.

ParameterDescription
sourcethe path of the CSS resource to push
mediathe 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.

ParameterDescription
pathsthe 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.

ParameterDescription
titlethe heading to show at the top of the error page
templatePaththe path of the template that could not be found
ethe exception describing the failure; its cause is shown in preference to the exception itself if it is a RuntimeException with a cause
outthe 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.

ParameterDescription
titlethe heading to show at the top of the error page
templatePaththe path of the template that failed to parse
rthe template resource whose body is shown in the error page
ethe parse exception describing the failure
outthe 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.

ParameterDescription
titlethe heading to show at the top of the error page
templatePaththe path of the template the error occurred in; if it contains a colon, only the part after it is used as the link target
templateBodythe raw template source to display with line numbers
desca description of the error to show on the page
linethe line number the error occurred at
columnthe column number the error occurred at
outthe 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.

ParameterDescription
templatePaththe path of the missing template
outthe 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.

ParameterDescription
resourcethe 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.

ParameterDescription
sourcethe 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.

ParameterDescription
templateTitlethe title pattern from the template, which may contain the token $title
pageTitlethe 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.

ParameterDescription
templateTitlethe title pattern from the template, which may contain the token $title
pageTitlethe page's own title to substitute into templateTitle
defaultTilethe 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.

To get full access to the Kademi Hub existing customers can login here, or new customers can register here.