Class PebbleTemplateImpl
- java.lang.Object
-
- io.pebbletemplates.pebble.template.PebbleTemplateImpl
-
- All Implemented Interfaces:
PebbleTemplate
public class PebbleTemplateImpl extends java.lang.Object implements PebbleTemplate
The actual implementation of a PebbleTemplate
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
PebbleTemplateImpl.NoopWriter
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.String,Block>
blocks
Blocks defined inside this template.private PebbleEngine
engine
A template has to store a reference to the main engine so that it can compile other templates when using the "import" or "include" tags.private java.util.Map<java.lang.String,Macro>
macros
Macros defined inside this template.private java.lang.String
name
Name of template.private RenderableNode
rootNode
The root node of the AST to be rendered.
-
Constructor Summary
Constructors Constructor Description PebbleTemplateImpl(PebbleEngine engine, RenderableNode root, java.lang.String name)
Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
block(java.io.Writer writer, EvaluationContextImpl context, java.lang.String blockName, boolean ignoreOverriden)
A typical block declaration will use this method which evaluates the block using the regular user-provided writer.void
embedTemplate(int lineNo, java.io.Writer writer, EvaluationContextImpl context, java.lang.String name, java.util.Map<?,?> additionalVariables, java.util.List<BlockNode> overriddenBlocks)
Embed a template withname
into this template and override its child blocks.void
evaluate(java.io.Writer writer)
Evaluate the template without any provided variables.private void
evaluate(java.io.Writer writer, EvaluationContextImpl context)
This is the authoritative evaluate method.void
evaluate(java.io.Writer writer, java.util.Locale locale)
Evaluate the template with a particular locale but without any provided variables.void
evaluate(java.io.Writer writer, java.util.Map<java.lang.String,java.lang.Object> map)
Evaluate the template with a set of variables and the default locale provided by thePebbleEngine
void
evaluate(java.io.Writer writer, java.util.Map<java.lang.String,java.lang.Object> map, java.util.Locale locale)
Evaluate the template with a particular locale and a set of variables.void
evaluateBlock(java.lang.String blockName, java.io.Writer writer)
Evaluate the template but only render the contents of a specific block.void
evaluateBlock(java.lang.String blockName, java.io.Writer writer, java.util.Locale locale)
Evaluate the template but only render the contents of a specific block.void
evaluateBlock(java.lang.String blockName, java.io.Writer writer, java.util.Map<java.lang.String,java.lang.Object> map)
Evaluate the template but only render the contents of a specific block.void
evaluateBlock(java.lang.String blockName, java.io.Writer writer, java.util.Map<java.lang.String,java.lang.Object> map, java.util.Locale locale)
Evaluate the template but only render the contents of a specific block.java.lang.String
getName()
Returns the template namePebbleTemplateImpl
getNamedImportedTemplate(EvaluationContextImpl context, java.lang.String alias)
Returns a named template.boolean
hasBlock(java.lang.String blockName)
Checks if a block existsboolean
hasMacro(java.lang.String macroName)
Checks if a macro existsvoid
importNamedMacrosFromTemplate(java.lang.String name, java.util.List<Pair<java.lang.String,java.lang.String>> namedMacros)
Imports named macros from specified template.void
importNamedTemplate(EvaluationContextImpl context, java.lang.String name, java.lang.String alias)
Imports a named template.void
importTemplate(EvaluationContextImpl context, java.lang.String name)
Imports a template.void
includeTemplate(java.io.Writer writer, EvaluationContextImpl context, java.lang.String name, java.util.Map<?,?> additionalVariables)
Includes a template withname
into this template.private EvaluationContextImpl
initContext(java.util.Locale locale)
Initializes the evaluation context with settings from the engine.SafeString
macro(EvaluationContextImpl context, java.lang.String macroName, ArgumentsNode args, boolean ignoreOverriden, int lineNumber)
Invokes a macrovoid
registerBlock(Block block)
Registers a block.void
registerMacro(Macro macro)
Registers a macrovoid
registerMacro(java.lang.String alias, Macro macro)
Registers a macro with aliasjava.lang.String
resolveRelativePath(java.lang.String relativePath)
This method resolves the given relative path based on this template file path.void
setParent(EvaluationContextImpl context, java.lang.String parentName)
private PebbleTemplateImpl
shallowCopy()
Return a shallow copy of this template.
-
-
-
Field Detail
-
engine
private final PebbleEngine engine
A template has to store a reference to the main engine so that it can compile other templates when using the "import" or "include" tags.It will also retrieve some stateful information such as the default locale when necessary. Luckily, the engine is immutable so this should be thread safe.
-
blocks
private final java.util.Map<java.lang.String,Block> blocks
Blocks defined inside this template.
-
macros
private final java.util.Map<java.lang.String,Macro> macros
Macros defined inside this template.
-
rootNode
private final RenderableNode rootNode
The root node of the AST to be rendered.
-
name
private final java.lang.String name
Name of template. Used to help with debugging.
-
-
Constructor Detail
-
PebbleTemplateImpl
public PebbleTemplateImpl(PebbleEngine engine, RenderableNode root, java.lang.String name)
Constructor- Parameters:
engine
- The pebble engine used to construct this templateroot
- The root not to evaluatename
- The name of the template
-
-
Method Detail
-
evaluate
public void evaluate(java.io.Writer writer) throws java.io.IOException
Description copied from interface:PebbleTemplate
Evaluate the template without any provided variables. This will use the default locale provided by thePebbleEngine
.- Specified by:
evaluate
in interfacePebbleTemplate
- Parameters:
writer
- The results of the evaluation are written to this writer.- Throws:
java.io.IOException
- An IO exception during the evaluation
-
evaluate
public void evaluate(java.io.Writer writer, java.util.Locale locale) throws java.io.IOException
Description copied from interface:PebbleTemplate
Evaluate the template with a particular locale but without any provided variables.- Specified by:
evaluate
in interfacePebbleTemplate
- Parameters:
writer
- The results of the evaluation are written to this writer.locale
- The locale used during the evaluation of the template.- Throws:
java.io.IOException
- An IO exception during the evaluation
-
evaluate
public void evaluate(java.io.Writer writer, java.util.Map<java.lang.String,java.lang.Object> map) throws java.io.IOException
Description copied from interface:PebbleTemplate
Evaluate the template with a set of variables and the default locale provided by thePebbleEngine
- Specified by:
evaluate
in interfacePebbleTemplate
- Parameters:
writer
- The results of the evaluation are written to this writer.map
- The variables used during the evaluation of the template.- Throws:
java.io.IOException
- An IO exception during the evaluation
-
evaluate
public void evaluate(java.io.Writer writer, java.util.Map<java.lang.String,java.lang.Object> map, java.util.Locale locale) throws java.io.IOException
Description copied from interface:PebbleTemplate
Evaluate the template with a particular locale and a set of variables.- Specified by:
evaluate
in interfacePebbleTemplate
- Parameters:
writer
- The results of the evaluation are written to this writer.map
- The variables used during the evaluation of the template.locale
- The locale used during the evaluation of the template.- Throws:
java.io.IOException
- An IO exception during the evaluation
-
evaluateBlock
public void evaluateBlock(java.lang.String blockName, java.io.Writer writer) throws java.io.IOException
Description copied from interface:PebbleTemplate
Evaluate the template but only render the contents of a specific block.- Specified by:
evaluateBlock
in interfacePebbleTemplate
- Parameters:
blockName
- The name of the template block to return.writer
- The results of the evaluation are written to this writer.- Throws:
java.io.IOException
- An IO exception during the evaluation
-
evaluateBlock
public void evaluateBlock(java.lang.String blockName, java.io.Writer writer, java.util.Locale locale) throws java.io.IOException
Description copied from interface:PebbleTemplate
Evaluate the template but only render the contents of a specific block.- Specified by:
evaluateBlock
in interfacePebbleTemplate
- Parameters:
blockName
- The name of the template block to return.writer
- The results of the evaluation are written to this writer.locale
- The locale used during the evaluation of the template.- Throws:
java.io.IOException
- An IO exception during the evaluation
-
evaluateBlock
public void evaluateBlock(java.lang.String blockName, java.io.Writer writer, java.util.Map<java.lang.String,java.lang.Object> map) throws java.io.IOException
Description copied from interface:PebbleTemplate
Evaluate the template but only render the contents of a specific block.- Specified by:
evaluateBlock
in interfacePebbleTemplate
- Parameters:
blockName
- The name of the template block to return.writer
- The results of the evaluation are written to this writer.map
- The variables used during the evaluation of the template.- Throws:
java.io.IOException
- An IO exception during the evaluation
-
evaluateBlock
public void evaluateBlock(java.lang.String blockName, java.io.Writer writer, java.util.Map<java.lang.String,java.lang.Object> map, java.util.Locale locale) throws java.io.IOException
Description copied from interface:PebbleTemplate
Evaluate the template but only render the contents of a specific block.- Specified by:
evaluateBlock
in interfacePebbleTemplate
- Parameters:
blockName
- The name of the template block to return.writer
- The results of the evaluation are written to this writer.map
- The variables used during the evaluation of the template.locale
- The locale used during the evaluation of the template.- Throws:
java.io.IOException
- An IO exception during the evaluation
-
evaluate
private void evaluate(java.io.Writer writer, EvaluationContextImpl context) throws java.io.IOException
This is the authoritative evaluate method. It will evaluate the template starting at the root node.- Parameters:
writer
- The writer used to write the final output of the templatecontext
- The evaluation context- Throws:
java.io.IOException
- Thrown from the writer object
-
initContext
private EvaluationContextImpl initContext(java.util.Locale locale)
Initializes the evaluation context with settings from the engine.- Parameters:
locale
- The desired locale- Returns:
- The evaluation context
-
shallowCopy
private PebbleTemplateImpl shallowCopy()
Return a shallow copy of this template.- Returns:
- A new template instance with the same data
-
importTemplate
public void importTemplate(EvaluationContextImpl context, java.lang.String name)
Imports a template.- Parameters:
context
- The evaluation contextname
- The template name
-
importNamedTemplate
public void importNamedTemplate(EvaluationContextImpl context, java.lang.String name, java.lang.String alias)
Imports a named template.- Parameters:
context
- The evaluation contextname
- The template namealias
- The template alias
-
importNamedMacrosFromTemplate
public void importNamedMacrosFromTemplate(java.lang.String name, java.util.List<Pair<java.lang.String,java.lang.String>> namedMacros)
Imports named macros from specified template.- Parameters:
name
- The template namenamedMacros
- named macros
-
getNamedImportedTemplate
public PebbleTemplateImpl getNamedImportedTemplate(EvaluationContextImpl context, java.lang.String alias)
Returns a named template.- Parameters:
context
- The evaluation contextalias
- The template alias
-
includeTemplate
public void includeTemplate(java.io.Writer writer, EvaluationContextImpl context, java.lang.String name, java.util.Map<?,?> additionalVariables) throws java.io.IOException
Includes a template withname
into this template.- Parameters:
writer
- the writer to which the output should be written to.context
- the context within which the template is rendered in.name
- the name of the template to include.additionalVariables
- the map with additional variables provided with the include tag to add within the include tag.- Throws:
java.io.IOException
- Any error during the loading of the template
-
embedTemplate
public void embedTemplate(int lineNo, java.io.Writer writer, EvaluationContextImpl context, java.lang.String name, java.util.Map<?,?> additionalVariables, java.util.List<BlockNode> overriddenBlocks) throws java.io.IOException
Embed a template withname
into this template and override its child blocks. This has the effect of essentially "including" a template (as with the `include` tag), but its blocks may be overridden in the calling template similar to extending a template.- Parameters:
lineNo
- the line number of the node being evaluatedwriter
- the writer to which the output should be written to.context
- the context within which the template is rendered in.name
- the name of the template to include.additionalVariables
- the map with additional variables provided with the include tag to add within the embed tag.overriddenBlocks
- the blocks parsed out of the parent template that should override blocks in the embedded template- Throws:
java.io.IOException
- Any error during the loading of the template
-
hasMacro
public boolean hasMacro(java.lang.String macroName)
Checks if a macro exists- Parameters:
macroName
- The name of the macro- Returns:
- Whether or not the macro exists
-
hasBlock
public boolean hasBlock(java.lang.String blockName)
Checks if a block exists- Parameters:
blockName
- The name of the block- Returns:
- Whether or not the block exists
-
resolveRelativePath
public java.lang.String resolveRelativePath(java.lang.String relativePath)
This method resolves the given relative path based on this template file path.- Parameters:
relativePath
- the path which should be resolved.- Returns:
- the resolved path.
-
registerBlock
public void registerBlock(Block block)
Registers a block.- Parameters:
block
- The block
-
registerMacro
public void registerMacro(Macro macro)
Registers a macro- Parameters:
macro
- The macro
-
registerMacro
public void registerMacro(java.lang.String alias, Macro macro)
Registers a macro with alias- Parameters:
macro
- The macro- Throws:
PebbleException
- Throws exception if macro already exists with the same name
-
block
public void block(java.io.Writer writer, EvaluationContextImpl context, java.lang.String blockName, boolean ignoreOverriden) throws java.io.IOException
A typical block declaration will use this method which evaluates the block using the regular user-provided writer.- Parameters:
blockName
- The name of the blockcontext
- The evaluation contextignoreOverriden
- Whether or not to ignore overriden blockswriter
- The writer- Throws:
java.io.IOException
- Thrown from the writer object
-
macro
public SafeString macro(EvaluationContextImpl context, java.lang.String macroName, ArgumentsNode args, boolean ignoreOverriden, int lineNumber)
Invokes a macro- Parameters:
context
- The evaluation contextmacroName
- The name of the macroargs
- The argumentsignoreOverriden
- Whether or not to ignore macro definitions in child template- Returns:
- The results of the macro invocation
-
setParent
public void setParent(EvaluationContextImpl context, java.lang.String parentName)
-
getName
public java.lang.String getName()
Returns the template name- Specified by:
getName
in interfacePebbleTemplate
- Returns:
- The name of the template
-
-