public class TemplateManager extends AbstractModule
A module where all Templates register themselves so that Actions, or other modules too, can find them. The other modules refer to the templates with their template key, not the module name. In addition, a template version may also be specified. The combination of key and version has to be unique, there can be several templates with same key but different versions. While the key must match exactly to the requested template key, version matching is not exact. Instead a best match is used. See getTemplate method documentation for more details.
Manager specific default context variables can be specified in the initialisation parameters using the parameter name prefix "context.". The rest of the parameter is the context variable name. Note that template specific parameters can similarly be defined in the Template modules too (assuming they derive from the AbstractTemplate class).
A templatePath initialisation parameter can be given, which should point to a directory containing template files. Templates are not required to use this, in fact, templates are not required to access any files at all, but it is good practice to respect it. However, note that VelocityTemplates in particular do not use this. Instead the template directory for velocity templates is defined in the VelocityEngineModule.
A TemplateContextProvider module can be used to provide the default context. This is an optional dependency and will be used if it is found in the module manager but not having one is not an error. You can set the ignoreContextProvider initialisation parameter to true to override this behaviour. Both a context provider and context initialisation parameters in this class can be used at the same time. These contexts are merged into one default context. In case of context keys clashing, initialisation parameters of this module override the values of the context provider.
Modifier and Type | Field and Description |
---|---|
protected TemplateContextProvider |
contextProvider |
protected java.util.HashMap<java.lang.String,java.lang.Object> |
defaultContext |
protected boolean |
ignoreContextProvider |
protected java.util.HashMap<java.lang.String,java.util.ArrayList<Template>> |
templateMap |
protected java.lang.String |
templatePath |
autoStart, isInitialized, isRunning, logging, loggingModule, moduleManager
Constructor and Description |
---|
TemplateManager() |
Modifier and Type | Method and Description |
---|---|
java.util.HashMap<java.lang.String,java.lang.Object> |
getDefaultContext()
Gets the manager specific default context which should be used
as a basis for template contexts.
|
java.util.Collection<Module> |
getDependencies(ModuleManager manager)
Returns all the modules this module depends on.
|
Template |
getTemplate(java.lang.String key,
java.lang.String version)
Gets the template using the specified key and version.
|
java.lang.String |
getTemplatePath()
Gets the specified path for template files.
|
void |
init(ModuleManager manager,
java.util.HashMap<java.lang.String,java.lang.Object> settings)
Initialises the module.
|
void |
putStaticContext(java.lang.String key,
java.lang.Object o)
Puts an object in the manager specific default template context.
|
void |
registerTemplate(Template template)
Register a template for this manager.
|
void |
start(ModuleManager manager)
Starts the module.
|
void |
stop(ModuleManager manager)
Stops the module.
|
void |
unregisterTemplate(Template template)
Unregisters a template.
|
isInitialized, isRunning, requireLogging, toString
protected final java.util.HashMap<java.lang.String,java.util.ArrayList<Template>> templateMap
protected final java.util.HashMap<java.lang.String,java.lang.Object> defaultContext
protected TemplateContextProvider contextProvider
protected boolean ignoreContextProvider
protected java.lang.String templatePath
public java.util.HashMap<java.lang.String,java.lang.Object> getDefaultContext()
public void putStaticContext(java.lang.String key, java.lang.Object o)
key
- The key of the template variable.o
- The value of the template variable.public void init(ModuleManager manager, java.util.HashMap<java.lang.String,java.lang.Object> settings) throws ModuleException
Module
Initialises the module. After constructor, this is the first method called in the life cycle of a module. It should not perform anything time consuming or anything with notable outside side effects. It should only read the parameters and initialise the module so that it can later be started. Note that a module being initialised doesn't mean that it necessarily will ever be started.
A ModuleException may be thrown if something vital is missing from the parameters or they are not sensible. In some cases you may not want to throw an exception even if vital initialisation information is missing. If, for example, it is possible that the module is initialised in some other way between the init and the start method calls. A ModuleException may also be thrown at the start method if the module is still not initialised.
init
in interface Module
init
in class AbstractModule
manager
- The module manager handling this module. You may keep a
reference to it if needed.ModuleException
public void stop(ModuleManager manager)
Module
stop
in interface Module
stop
in class AbstractModule
manager
- The module manager handling this module.public void start(ModuleManager manager) throws ModuleException
Module
start
in interface Module
start
in class AbstractModule
manager
- The module manager handling this module.ModuleException
public java.lang.String getTemplatePath()
public java.util.Collection<Module> getDependencies(ModuleManager manager) throws ModuleException
Module
getDependencies
in interface Module
getDependencies
in class AbstractModule
manager
- The module manager handling this module.ModuleException
public Template getTemplate(java.lang.String key, java.lang.String version)
key
- The key of the template to get.version
- The version of the template, or null if not applicable.public void registerTemplate(Template template)
template
- The template to register.public void unregisterTemplate(Template template)
template
- The template to unregister.Copyright 2004-2015 Wandora Team