类 MiniTemplator
Template syntax:
Variables:
${VariableName}
Blocks:
<!-- $beginBlock blockName -->
... block contents ...
<!-- $endBlock blockName -->
Conditional blocks:
<!-- $if flag1 flag2 -->
... included if flag1 or flag2 is set ...
<!-- $elseIf !flag3 flag4 -->
... included if flag3 is not set or flag4 is set ...
<!-- $else -->
... included if none of the above conditions is met ...
<!-- $endIf -->
Short form of conditional blocks:
(only recognized if TemplateSpecification.shortFormEnabled is true)
<$? flag1 flag2 >
... included if flag1 or flag2 is set ...
<$: !flag3 flag4 >
... included if flag3 is not set or flag4 is set ...
<$:>
... included if none of the above conditions is met ...
<$/?>
Example:
<$?de> Hallo Welt!
<$:fr> Bonjour tout le monde!
<$: > Hello world!
<$/?>
Include a subtemplate:
<!-- $include relativeFileName -->
General remarks:
- Variable names, block names, condition flags and commands (e.g. "$beginBlock") are case-insensitive.
- The same variable may be used multiple times within a template.
- Multiple blocks with the same name may occur within a template.
- Blocks can be nested.
- Conditional blocks ($if) and includes ($include) are resolved when the template is parsed. Parsing is done within
the MiniTemplator constructor. Condition flags can be passed to the constructor using
MiniTemplator.TemplateSpecification. - Normal blocks ($beginBlock) must be added (and can be repeated) by the application program using
addBlock(). - The
MiniTemplatorCacheclass may be used to cache MiniTemplator objects with parsed templates.
Project home page: www.source-code.biz/MiniTemplator
Author: Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland
-
嵌套类概要
嵌套类修饰符和类型类说明static classThrown whenMinitemplator.addBlockis called with ablockNamethat is not defined within the template.static classSpecifies the parameters for constructing aMiniTemplatorobject.static classThrown when a syntax error is encountered within the template.static classThrown whenMinitemplator.setVariableis called with avariableNamethat is not defined within the template and theisOptionalparameter isfalse. -
构造器概要
构造器限定符构造器说明protectedDummy constructor, used internally in newInstance().MiniTemplator(MiniTemplator.TemplateSpecification templateSpec) Constructs a MiniTemplator object.MiniTemplator(String templateFileName, InputStream templateFileStream) Constructs a MiniTemplator object by specifying only the file name. -
方法概要
修饰符和类型方法说明voidAdds an instance of a template block.voidAdds an instance of a template block.voidaddBlockOpt(String blockName) Adds an instance of an optional template block.booleanblockExists(String blockName) Checks whether a block with the specified name exists within the template.Clones this MiniTemplator object and resets the clone.static StringescapeHtml(String s) Escapes special HTML characters.Generates the HTML page and returns it as a string.voidgenerateOutput(Writer outputWriter) Generates the HTML page and writes it to a character stream.voidgenerateOutput(String outputFileName) Generates the HTML page and writes it into a file.Returns a map with the names and current values of the template variables.protected StringloadSubtemplate(String subtemplateName) Loads the template string of a subtemplate (used for the $Include command).protected MiniTemplatorAllocates a new uninitialized MiniTemplator object.voidreset()Resets the MiniTemplator object to the initial state.voidsetVariable(String variableName, int variableValue) Sets a template variable to an integer value.voidsetVariable(String variableName, String variableValue) Sets a template variable.voidsetVariable(String variableName, String variableValue, boolean isOptional) Sets a template variable.voidsetVariableEsc(String variableName, String variableValue) Sets a template variable to an escaped value.voidsetVariableEsc(String variableName, String variableValue, boolean isOptional) Sets a template variable to an escaped value.voidsetVariableOpt(String variableName, int variableValue) Sets an optional template variable to an integer value.voidsetVariableOpt(String variableName, String variableValue) Sets an optional template variable.voidsetVariableOptEsc(String variableName, String variableValue) Sets an optional template variable to an escaped value.booleanvariableExists(String variableName) Checks whether a variable with the specified name exists within the template.
-
构造器详细资料
-
MiniTemplator
public MiniTemplator(MiniTemplator.TemplateSpecification templateSpec) throws IOException, MiniTemplator.TemplateSyntaxException Constructs a MiniTemplator object.During construction, the template and subtemplate files are read and parsed.
Note: The
MiniTemplatorCacheclass may be used to cache MiniTemplator objects.- 参数:
templateSpec- the template specification.- 抛出:
MiniTemplator.TemplateSyntaxException- when a syntax error is detected within the template.IOException- when an i/o error occurs while reading the template.
-
MiniTemplator
public MiniTemplator(String templateFileName, InputStream templateFileStream) throws IOException, MiniTemplator.TemplateSyntaxException Constructs a MiniTemplator object by specifying only the file name.This is a convenience constructor that may be used when only the file name has to be specified.
- 参数:
templateFileName- the file name of the template file.- 抛出:
MiniTemplator.TemplateSyntaxException- when a syntax error is detected within the template.IOException- when an i/o error occurs while reading the template.- 另请参阅:
-
MiniTemplator
protected MiniTemplator()Dummy constructor, used internally in newInstance().
-
-
方法详细资料
-
newInstance
Allocates a new uninitialized MiniTemplator object. This method is intended to be overridden in a derived class. It is called from cloneReset() to create a new MiniTemplator object. -
loadSubtemplate
Loads the template string of a subtemplate (used for the $Include command). This method can be overridden in a subclass, to load subtemplates from somewhere else, e.g. from a database.This implementation of the method interprets
subtemplateNameas a relative file path name and reads the template string from that file.MiniTemplator.TemplateSpecification.subtemplateBasePathis used to convert the relative path of the subtemplate into an absolute path.- 参数:
subtemplateName- the name of the subtemplate. Normally a relative file path. This is the argument string that was specified with the "$Include" command. If the string has quotes, the quotes are removed before this method is called.- 返回:
- the template text string of the subtemplate.
- 抛出:
IOException
-
reset
public void reset()Resets the MiniTemplator object to the initial state. All variable values are cleared and all added block instances are deleted. This method can be used to produce another HTML page with the same template. It is faster than creating another MiniTemplator object, because the template does not have to be read and parsed again. -
cloneReset
Clones this MiniTemplator object and resets the clone. This method is used to copy a MiniTemplator object. It is fast, because the template does not have to be parsed again, and the internal data structures that contain the parsed template information are shared among the clones.This method is used by the
MiniTemplatorCacheclass to clone the cached MiniTemplator objects. -
setVariable
public void setVariable(String variableName, String variableValue, boolean isOptional) throws MiniTemplator.VariableNotDefinedException Sets a template variable.For variables that are used in blocks, the variable value must be set before
addBlock()is called.- 参数:
variableName- the name of the variable to be set. Case-insensitive.variableValue- the new value of the variable. May benull.isOptional- specifies whether an exception should be thrown when the variable does not exist in the template. IfisOptionalisfalseand the variable does not exist, an exception is thrown.- 抛出:
MiniTemplator.VariableNotDefinedException- when no variable with the specified name exists in the template andisOptionalisfalse.
-
setVariable
public void setVariable(String variableName, String variableValue) throws MiniTemplator.VariableNotDefinedException Sets a template variable.Convenience method for:
setVariable (variableName, variableValue, false)- 参数:
variableName- the name of the variable to be set. Case-insensitive.variableValue- the new value of the variable. May benull.- 抛出:
MiniTemplator.VariableNotDefinedException- when no variable with the specified name exists in the template.- 另请参阅:
-
setVariable
public void setVariable(String variableName, int variableValue) throws MiniTemplator.VariableNotDefinedException Sets a template variable to an integer value.Convenience method for:
setVariable (variableName, Integer.toString(variableValue))- 参数:
variableName- the name of the variable to be set. Case-insensitive.variableValue- the new value of the variable.- 抛出:
MiniTemplator.VariableNotDefinedException- when no variable with the specified name exists in the template.
-
setVariableOpt
Sets an optional template variable.Convenience method for:
setVariable (variableName, variableValue, true)- 参数:
variableName- the name of the variable to be set. Case-insensitive.variableValue- the new value of the variable. May benull.- 另请参阅:
-
setVariableOpt
Sets an optional template variable to an integer value.Convenience method for:
setVariableOpt (variableName, Integer.toString(variableValue))- 参数:
variableName- the name of the variable to be set. Case-insensitive.variableValue- the new value of the variable.
-
setVariableEsc
public void setVariableEsc(String variableName, String variableValue, boolean isOptional) throws MiniTemplator.VariableNotDefinedException Sets a template variable to an escaped value.Convenience method for:
setVariable (variableName, MiniTemplator.escapeHtml(variableValue), isOptional)- 参数:
variableName- the name of the variable to be set.variableValue- the new value of the variable. May benull. Special HTML/XML characters are escaped.isOptional- specifies whether an exception should be thrown when the variable does not exist in the template. IfisOptionalisfalseand the variable does not exist, an exception is thrown.- 抛出:
MiniTemplator.VariableNotDefinedException- when no variable with the specified name exists in the template andisOptionalisfalse.- 另请参阅:
-
setVariableEsc
public void setVariableEsc(String variableName, String variableValue) throws MiniTemplator.VariableNotDefinedException Sets a template variable to an escaped value.Convenience method for:
setVariable (variableName, MiniTemplator.escapeHtml(variableValue), false)- 参数:
variableName- the name of the variable to be set. Case-insensitive.variableValue- the new value of the variable. May benull. Special HTML/XML characters are escaped.- 抛出:
MiniTemplator.VariableNotDefinedException- when no variable with the specified name exists in the template.- 另请参阅:
-
setVariableOptEsc
Sets an optional template variable to an escaped value.Convenience method for:
setVariable (variableName, MiniTemplator.escapeHtml(variableValue), true)- 参数:
variableName- the name of the variable to be set. Case-insensitive.variableValue- the new value of the variable. May benull. Special HTML/XML characters are escaped.- 另请参阅:
-
variableExists
Checks whether a variable with the specified name exists within the template.- 参数:
variableName- the name of the variable. Case-insensitive.- 返回:
trueif the variable exists.
falseif no variable with the specified name exists in the template.
-
getVariables
Returns a map with the names and current values of the template variables. -
addBlock
public void addBlock(String blockName, boolean isOptional) throws MiniTemplator.BlockNotDefinedException Adds an instance of a template block.If the block contains variables, these variables must be set before the block is added. If the block contains subblocks (nested blocks), the subblocks must be added before this block is added. If multiple blocks exist with the specified name, an instance is added for each block occurrence.
- 参数:
blockName- the name of the block to be added. Case-insensitive.isOptional- specifies whether an exception should be thrown when the block does not exist in the template. IfisOptionalisfalseand the block does not exist, an exception is thrown.- 抛出:
MiniTemplator.BlockNotDefinedException- when no block with the specified name exists in the template andisOptionalisfalse.
-
addBlock
Adds an instance of a template block.Convenience method for:
addBlock (blockName, false)- 参数:
blockName- the name of the block to be added. Case-insensitive.- 抛出:
MiniTemplator.BlockNotDefinedException- when no block with the specified name exists in the template.- 另请参阅:
-
addBlockOpt
Adds an instance of an optional template block.Convenience method for:
addBlock (blockName, true)- 参数:
blockName- the name of the block to be added. Case-insensitive.- 另请参阅:
-
blockExists
Checks whether a block with the specified name exists within the template.- 参数:
blockName- the name of the block.- 返回:
trueif the block exists.
falseif no block with the specified name exists in the template.
-
generateOutput
Generates the HTML page and writes it into a file.- 参数:
outputFileName- name of the file to which the generated HTML page will be written.- 抛出:
IOException- when an i/o error occurs while writing to the file.
-
generateOutput
Generates the HTML page and writes it to a character stream.- 参数:
outputWriter- a character stream (writer) to which the HTML page will be written.- 抛出:
IOException- when an i/o error occurs while writing to the stream.
-
generateOutput
Generates the HTML page and returns it as a string.- 返回:
- A string that contains the generated HTML page.
-
escapeHtml
Escapes special HTML characters. Replaces the characters <, >, &, ' and " by their corresponding HTML/XML character entity codes.- 参数:
s- the input string.- 返回:
- the escaped output string.
-