Renders pages by combining a theme with a content template using Apache Velocity, and is the default HtmlTemplater implementation used to serve website and admin pages. A theme supplies the page skeleton, ie the surrounding html, head and body markup, plus web resources such as JavaScript and CSS files to include in the header and one or more body layouts; the content template supplies the body content for the specific page, together with any further web resources it needs and a parameter naming which theme layout to use. Parsed template metadata is cached in memory, keyed by root folder and theme, so repeated requests for the same page avoid reparsing. An instance of this class is also exposed to Velocity templates as the "templater" variable, giving theme authors access to methods such as pushJs, pushCss and getCombinedJsPaths for registering and combining a page's JavaScript and CSS dependencies.
Implements: HtmlTemplater
Properties
| Property | Returns | Description |
|---|---|---|
| cssSources | Map<String,List<String>> | CSS sources registered so far in the current request via pushCss, grouped by media type. Null if called outside of an HTTP request, or if nothing has been registered yet. |
| cssSourcesList | List<String> | CSS sources registered so far in the current request via pushCss, flattened into a single list across all media types. Unlike JS sources, CSS sources are not ordering-dependent so grouping is not preserved. |
| engine | VelocityEngine | The underlying Apache Velocity engine used to compile and cache theme and content templates. |
| jsSources | Map<String,List<String>> | JavaScript sources registered so far in the current request via pushJs, grouped by group name and ordered within each group by the position given to pushJs. Empty if called outside of an HTTP request, or if nothing has been registered yet. |
| templateParser | HtmlTemplateParser | The parser used to turn a raw theme or content template into its metadata, ie its declared web resources and body, before the result is cached. |
| templateRenderer | HtmlTemplateRenderer | The renderer that combines a resolved theme template and content template, plus their web resources, into the final HTML page. |
| webRoot | String | The root path templates are resolved against. Defaults to "/". |
Methods
getCssPaths(FileResource page) · getWebRoot() · getTemplate(String templatePath) · pushJs(String source) · pushJs(String source, String group) · pushJs(String source, String group, Integer position) · pushCss(String source) · pushCss(String source, String media) · getCssSources() · getCssSourcesList() · getJsSources() · getJsSources(String group) · getCombinedJsPath(List<String> paths) · getCombinedJsPath(String group) · getCombinedJsPaths(String group) · pushLess(String source, String media) · pushLess(String source, String media, Integer pos) · getEngine() · getTemplateRenderer() · getTemplateParser() · getClassName(Object resource)
getCssPaths(FileResource page)
Returns: List<String>
Works out the CSS files a rendered file page depends on, by resolving the page's body and theme templates and combining the screen CSS web resources declared in each.
| Parameter | Description |
|---|---|
page | the page to find CSS dependencies for |
getWebRoot()
Returns: String
The root path templates are resolved against. Defaults to "/".
getTemplate(String templatePath)
Returns: Template
Loads the compiled Velocity template for the given source path via the underlying VelocityEngine, which caches and re-parses it as needed based on the resource's modification time.
| Parameter | Description |
|---|---|
templatePath | the source path of the template to load, must not be null |
pushJs(String source)
Returns: String
Registers a JavaScript file to be included in the rendered page, in the "default" group and with no preferred position. Equivalent to calling the three-argument overload of this method.
| Parameter | Description |
|---|---|
source | path of the JavaScript file to include |
pushJs(String source, String group)
Returns: String
Registers a JavaScript file to be included in the rendered page under the given group, with no preferred position. Files in the same group are combined into a single request when the page is rendered.
| Parameter | Description |
|---|---|
source | path of the JavaScript file to include |
group | the group the source is added to, used to combine sources together at render time |
pushJs(String source, String group, Integer position)
Returns: String
Registers a JavaScript file to be included in the rendered page under the given group and position, for the duration of the current request. Duplicate registrations of the same source and position within a group are ignored. Does nothing if called outside of an HTTP request.
| Parameter | Description |
|---|---|
source | path of the JavaScript file to include |
group | the group the source is added to, used to combine sources together at render time |
position | the source's preferred position within the group, or null to leave it unordered |
pushCss(String source)
Returns: String
Registers a CSS file to be included in the rendered page, with no specific media type. Equivalent to calling the two-argument overload of this method with a null media.
| Parameter | Description |
|---|---|
source | path of the CSS file to include |
pushCss(String source, String media)
Returns: String
Registers a CSS file to be included in the rendered page under the given media type, for the duration of the current request. Files sharing a media type are grouped together when the page is rendered. Does nothing if called outside of an HTTP request.
| Parameter | Description |
|---|---|
source | path of the CSS file to include |
media | the CSS media attribute to group the source under, or null for the default (all) media |
getCssSources()
Returns: Map<String,List<String>>
CSS sources registered so far in the current request via pushCss, grouped by media type. Null if called outside of an HTTP request, or if nothing has been registered yet.
getCssSourcesList()
Returns: List<String>
CSS sources registered so far in the current request via pushCss, flattened into a single list across all media types. Unlike JS sources, CSS sources are not ordering-dependent so grouping is not preserved.
getJsSources()
Returns: Map<String,List<String>>
JavaScript sources registered so far in the current request via pushJs, grouped by group name and ordered within each group by the position given to pushJs. Empty if called outside of an HTTP request, or if nothing has been registered yet.
getJsSources(String group)
Returns: List<String>
JavaScript sources registered so far in the current request via pushJs under the given group, ordered by the position given to pushJs. Empty if called outside of an HTTP request, or if nothing has been registered for that group yet.
| Parameter | Description |
|---|---|
group | the group to look up registered sources for |
getCombinedJsPath(List<String> paths)
Returns: String
Builds a single combined-resource URL that represents all of the given JavaScript paths, versioned with the current root folder's content hash so a change to any of the underlying files busts the cache.
| Parameter | Description |
|---|---|
paths | the JavaScript source paths to combine |
getCombinedJsPath(String group)
Returns: String
Builds a single combined-resource URL for all JavaScript sources registered under the given group in the current request.
| Parameter | Description |
|---|---|
group | the group whose registered sources are combined |
getCombinedJsPaths(String group)
Returns: List<String>
Combines the JavaScript sources registered under the given group in the current request into one or more combined-resource URLs, capped at a maximum length. If the sources would exceed that length as a single combined path, they are split across multiple combined paths instead.
| Parameter | Description |
|---|---|
group | the group whose registered sources are combined |
pushLess(String source, String media)
Returns: String
Not implemented in this templater. Templates may call this to register a LESS source, but this implementation always returns an empty string and does not record the source.
| Parameter | Description |
|---|---|
source | path of the LESS file that would be included |
media | the CSS media attribute that would apply to the source |
pushLess(String source, String media, Integer pos)
Returns: String
Not implemented in this templater. Templates may call this to register a LESS source, but this implementation always returns an empty string and does not record the source.
| Parameter | Description |
|---|---|
source | path of the LESS file that would be included |
media | the CSS media attribute that would apply to the source |
pos | the source's preferred position that would apply if registration were implemented |
getEngine()
Returns: VelocityEngine
The underlying Apache Velocity engine used to compile and cache theme and content templates.
getTemplateRenderer()
Returns: HtmlTemplateRenderer
The renderer that combines a resolved theme template and content template, plus their web resources, into the final HTML page.
getTemplateParser()
Returns: HtmlTemplateParser
The parser used to turn a raw theme or content template into its metadata, ie its declared web resources and body, before the result is cached.
getClassName(Object resource)
Returns: String
The unqualified class name of the given object, with any package prefix stripped off. Used by templates, for example to render a page type marker for diagnostic or styling purposes.
| Parameter | Description |
|---|---|
resource | the object whose class name is looked up |