Package groovy.text.markup
Class MarkupTemplateEngine
java.lang.Object
groovy.text.TemplateEngine
groovy.text.markup.MarkupTemplateEngine
A template engine which leverages
StreamingMarkupBuilder to generate XML/XHTML.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA template resolver which avoids callingClassLoader.getResource(String)if a template path already has been queried before.static classDefault resolver that loads templates from the configured template class loader and applies locale fallback rules.static classDescribes a template resource name split into base name, optional locale suffix, and extension. -
Constructor Summary
ConstructorsConstructorDescriptionCreates an engine with the defaultTemplateConfiguration.Creates an engine with the supplied template configuration.MarkupTemplateEngine(ClassLoader parentLoader, TemplateConfiguration config) Creates an engine with the supplied parent loader and template configuration.MarkupTemplateEngine(ClassLoader parentLoader, TemplateConfiguration config, TemplateResolver resolver) Creates an engine with full control over the class loader, configuration, and resolver strategy.MarkupTemplateEngine(ClassLoader parentLoader, File templateDirectory, TemplateConfiguration tplConfig) Convenience constructor to build a template engine which searches for templates into a directory -
Method Summary
Modifier and TypeMethodDescriptioncreateTemplate(Reader reader) Compiles a template from a reader.createTemplate(Reader reader, String sourceName) Compiles a template from a reader using an explicit source name.createTemplate(URL resource) Compiles a template from a resolved resource URL.createTemplateByPath(String templatePath) Resolves and compiles a template by path.Compiles template source from a reader with explicit model type hints and a source name.createTypeCheckedModelTemplate(Reader reader, Map<String, String> modelTypes) Compiles template source from a reader with explicit model type hints.Compiles inline template source with explicit model type hints and a source name.createTypeCheckedModelTemplate(String source, Map<String, String> modelTypes) Compiles inline template source with explicit model type hints.createTypeCheckedModelTemplate(URL resource, Map<String, String> modelTypes) Compiles a template from a resolved resource URL with explicit model type hints.createTypeCheckedModelTemplateByPath(String templatePath, Map<String, String> modelTypes) Resolves and compiles a template by path with explicit model type hints.Returns the Groovy compiler configuration used for generated template classes.Returns the rendering configuration used by this engine.Returns the class loader used to compile generated template classes.resolveTemplate(String templatePath) Resolves a logical template path into a concrete resource URL.Methods inherited from class groovy.text.TemplateEngine
createTemplate, createTemplate, createTemplate, createTemplate
-
Constructor Details
-
MarkupTemplateEngine
public MarkupTemplateEngine()Creates an engine with the defaultTemplateConfiguration. -
MarkupTemplateEngine
Creates an engine with the supplied template configuration.- Parameters:
config- template configuration to apply while compiling and rendering templates
-
MarkupTemplateEngine
Creates an engine with the supplied parent loader and template configuration.- Parameters:
parentLoader- class loader used to compile generated template classesconfig- template configuration to apply while compiling and rendering templates
-
MarkupTemplateEngine
public MarkupTemplateEngine(ClassLoader parentLoader, TemplateConfiguration config, TemplateResolver resolver) Creates an engine with full control over the class loader, configuration, and resolver strategy.- Parameters:
parentLoader- class loader used to compile generated template classesconfig- template configuration to apply while compiling and rendering templatesresolver- template resolver to use, ornullfor the default resolver
-
MarkupTemplateEngine
public MarkupTemplateEngine(ClassLoader parentLoader, File templateDirectory, TemplateConfiguration tplConfig) Convenience constructor to build a template engine which searches for templates into a directory- Parameters:
templateDirectory- directory where to find templatestplConfig- template engine configuration
-
-
Method Details
-
createTemplate
public Template createTemplate(Reader reader) throws CompilationFailedException, ClassNotFoundException, IOException Compiles a template from a reader.- Specified by:
createTemplatein classTemplateEngine- Parameters:
reader- template source- Returns:
- a compiled markup template
- Throws:
CompilationFailedException- if Groovy fails to compile the generated templateClassNotFoundException- if the generated template class cannot be instantiatedIOException- if reading the template source fails
-
createTemplate
public Template createTemplate(Reader reader, String sourceName) throws CompilationFailedException, ClassNotFoundException, IOException Compiles a template from a reader using an explicit source name.- Parameters:
reader- template sourcesourceName- source name used for diagnostics and generated classes- Returns:
- a compiled markup template
- Throws:
CompilationFailedException- if Groovy fails to compile the generated templateClassNotFoundException- if the generated template class cannot be instantiatedIOException- if reading the template source fails
-
createTemplateByPath
public Template createTemplateByPath(String templatePath) throws CompilationFailedException, ClassNotFoundException, IOException Resolves and compiles a template by path.- Parameters:
templatePath- logical template path understood by the configured resolver- Returns:
- a compiled markup template
- Throws:
CompilationFailedException- if Groovy fails to compile the generated templateClassNotFoundException- if the generated template class cannot be instantiatedIOException- if template resolution or reading fails
-
createTypeCheckedModelTemplate
public Template createTypeCheckedModelTemplate(String source, Map<String, String> modelTypes) throws CompilationFailedException, ClassNotFoundException, IOExceptionCompiles inline template source with explicit model type hints.- Parameters:
source- template sourcemodelTypes- model variable type hints keyed by variable name- Returns:
- a compiled markup template
- Throws:
CompilationFailedException- if Groovy fails to compile the generated templateClassNotFoundException- if the generated template class cannot be instantiatedIOException- if reading the template source fails
-
createTypeCheckedModelTemplate
public Template createTypeCheckedModelTemplate(String source, String sourceName, Map<String, String> modelTypes) throws CompilationFailedException, ClassNotFoundException, IOExceptionCompiles inline template source with explicit model type hints and a source name.- Parameters:
source- template sourcesourceName- source name used for diagnostics and generated classesmodelTypes- model variable type hints keyed by variable name- Returns:
- a compiled markup template
- Throws:
CompilationFailedException- if Groovy fails to compile the generated templateClassNotFoundException- if the generated template class cannot be instantiatedIOException- if reading the template source fails
-
createTypeCheckedModelTemplate
public Template createTypeCheckedModelTemplate(Reader reader, Map<String, String> modelTypes) throws CompilationFailedException, ClassNotFoundException, IOExceptionCompiles template source from a reader with explicit model type hints.- Parameters:
reader- template sourcemodelTypes- model variable type hints keyed by variable name- Returns:
- a compiled markup template
- Throws:
CompilationFailedException- if Groovy fails to compile the generated templateClassNotFoundException- if the generated template class cannot be instantiatedIOException- if reading the template source fails
-
createTypeCheckedModelTemplate
public Template createTypeCheckedModelTemplate(Reader reader, String sourceName, Map<String, String> modelTypes) throws CompilationFailedException, ClassNotFoundException, IOExceptionCompiles template source from a reader with explicit model type hints and a source name.- Parameters:
reader- template sourcesourceName- source name used for diagnostics and generated classesmodelTypes- model variable type hints keyed by variable name- Returns:
- a compiled markup template
- Throws:
CompilationFailedException- if Groovy fails to compile the generated templateClassNotFoundException- if the generated template class cannot be instantiatedIOException- if reading the template source fails
-
createTypeCheckedModelTemplateByPath
public Template createTypeCheckedModelTemplateByPath(String templatePath, Map<String, String> modelTypes) throws CompilationFailedException, ClassNotFoundException, IOExceptionResolves and compiles a template by path with explicit model type hints.- Parameters:
templatePath- logical template path understood by the configured resolvermodelTypes- model variable type hints keyed by variable name- Returns:
- a compiled markup template
- Throws:
CompilationFailedException- if Groovy fails to compile the generated templateClassNotFoundException- if the generated template class cannot be instantiatedIOException- if template resolution or reading fails
-
createTemplate
public Template createTemplate(URL resource) throws CompilationFailedException, ClassNotFoundException, IOException Compiles a template from a resolved resource URL.- Overrides:
createTemplatein classTemplateEngine- Parameters:
resource- template resource- Returns:
- a compiled markup template
- Throws:
CompilationFailedException- if Groovy fails to compile the generated templateClassNotFoundException- if the generated template class cannot be instantiatedIOException- if loading the resource fails
-
createTypeCheckedModelTemplate
public Template createTypeCheckedModelTemplate(URL resource, Map<String, String> modelTypes) throws CompilationFailedException, ClassNotFoundException, IOExceptionCompiles a template from a resolved resource URL with explicit model type hints.- Parameters:
resource- template resourcemodelTypes- model variable type hints keyed by variable name- Returns:
- a compiled markup template
- Throws:
CompilationFailedException- if Groovy fails to compile the generated templateClassNotFoundException- if the generated template class cannot be instantiatedIOException- if loading the resource fails
-
getTemplateLoader
Returns the class loader used to compile generated template classes.- Returns:
- the template class loader
-
getCompilerConfiguration
Returns the Groovy compiler configuration used for generated template classes.- Returns:
- the compiler configuration
-
getTemplateConfiguration
Returns the rendering configuration used by this engine.- Returns:
- the template configuration
-
resolveTemplate
Resolves a logical template path into a concrete resource URL.- Parameters:
templatePath- logical template path- Returns:
- the resolved template resource
- Throws:
IOException- if the template cannot be resolved
-