public interface Loader<T>
Modifier and Type | Method and Description |
---|---|
T |
createCacheKey(String templateName)
This method resolves the given template name to a unique object which can be used as the key
within the
PebbleEngine.getTemplateCache() . |
Reader |
getReader(T cacheKey)
The reader which will be used by Pebble to read the contents of the template.
|
String |
resolveRelativePath(String relativePath,
String anchorPath)
Resolves the given
relativePath based on the given anchorPath . |
void |
setCharset(String charset)
A method for end users to change the charset used by the loader.
|
void |
setPrefix(String prefix)
Optional prefix to help find templates, ex "/WEB-INF/templates/" or "database_schema."
|
void |
setSuffix(String suffix)
Optional suffix to help find templates, ex ".html", ".peb"
|
Reader getReader(T cacheKey)
cacheKey
- the cache key to use to load create the reader.void setCharset(String charset)
charset
- Character set used by the loader when building a reader objectvoid setPrefix(String prefix)
prefix
- Prefix to help find templatesvoid setSuffix(String suffix)
suffix
- Suffix to attach to template namesString resolveRelativePath(String relativePath, String anchorPath)
relativePath
based on the given anchorPath
.
A path is considered as relative when it starts either with '..' or '.' and followed either by a '/' or '\\' otherwise the assumption is that the provided path is an absolute path.
relativePath
- the relative path which should be resolved.anchorPath
- the anchor path based on which the relative path should be resolved on.null
when the path could not be resolved.T createCacheKey(String templateName)
PebbleEngine.getTemplateCache()
. The returned object will be passed with
getReader(Object)
.
The resolve method can eventually add information to the cache key from the context (e.g. user session information, servlet request etc.).
As a concrete example if the loader loads a template created by a user form the database the template name itself is not uniquely identify the template. The identification of the template requires also the user which created the template. Hence for the key the user id and the template name should be used. So the cache key is enhanced by some contextual information.
The implementor of the method can add as many additional contextual information to the returned object. However the following things needs to be considered:
PebbleEngine.getTemplate(String)
. Hence the implementation needs to be fast and
eventually use some caching for the lookup process.Object.equals(Object)
and Object.hashCode()
.
Depending on this implementation the PebbleEngine.getTemplateCache()
should be tuned in
a way it can operate optimal. E.g. when the number of potential templates is infinite the cache
should evict some templates at some point in time otherwise the stability of the memory is not
given anymore.
templateName
- The name of the templateCopyright © 2018. All rights reserved.