| java.lang.Object freemarker.core.Configurable freemarker.core.Environment
Environment | final public class Environment extends Configurable (Code) | | Object that represents the runtime environment during template processing.
For every invocation of a Template.process() method, a new instance
of this object is created, and then discarded when process() returns.
This object stores the set of temporary variables created by the template,
the value of settings set by the template, the reference to the data model root,
etc. Everything that is needed to fulfill the template processing job.
Data models that need to access the Environment
object that represents the template processing on the current thread can use
the
Environment.getCurrentEnvironment() method.
If you need to modify or read this object before or after the process
call, use
Template.createProcessingEnvironment(Object rootMapWriter outObjectWrapper wrapper) author: Jonathan Revusky author: Attila Szegedi |
Inner Class :public class Namespace extends SimpleHash | |
Method Summary | |
public Object | __getitem__(String key) A hook that Jython uses. | public void | __setitem__(String key, Object o) A hook that Jython uses. | void | clearLastReturnValue() | void | fallback() | String | formatDate(Date date, int type) | String | formatNumber(Number number) | Collator | getCollator() | public Configuration | getConfiguration() | public static Environment | getCurrentEnvironment() Retrieves the environment object associated with the current
thread. | Macro.Context | getCurrentMacroContext() | public Namespace | getCurrentNamespace() Returns the main name-space. | String | getCurrentRecoveredErrorMesssage() | public TemplateNodeModel | getCurrentVisitorNode() | public TemplateHashModel | getDataModel() Returns the data model hash. | DateFormat | getDateFormatObject(int dateType) | DateFormat | getDateFormatObject(int dateType, String pattern) | public String | getDefaultNS() | String | getEffectiveURLEscapingCharset() Returns the name of the charset that should be used for URL encoding.
This will be null if the information is not available.
The function caches the return value, so it is quick to call it
repeately. | public Namespace | getGlobalNamespace() Returns a fictitious name-space that contains the globally visible variables
that were created in the template, but not the variables of the data-model. | public TemplateModel | getGlobalVariable(String name) Returns the globally visible variable of the given name (or null). | public TemplateHashModel | getGlobalVariables() Returns the read-only hash of globally visible variables. | public Set | getKnownVariableNames() Returns a set of variable names that are known at the time of call. | TemplateModel | getLastReturnValue() | ArrayList | getLocalContextStack() | public TemplateModel | getLocalVariable(String name) Returns the loop or macro local variable corresponding to this
variable name. | Namespace | getMacroNamespace(Macro macro) | public Namespace | getMainNamespace() Returns the main name-space. | public Namespace | getNamespace(String name) Returns the name-space for the name if exists, or null.
Parameters: name - the template path that you have used with the import directiveor Environment.importLib(String,String) call, in normalized form. | public String | getNamespaceForPrefix(String prefix) | TemplateModel | getNodeProcessor(TemplateNodeModel node) | NumberFormat | getNumberFormatObject(String pattern) | public Writer | getOut() | public String | getPrefixForNamespace(String nsURI) | public Template | getTemplate() Retrieves the currently processed template. | public Template | getTemplateForImporting(String name) Gets a template for importing; used with
Environment.importLib(Template importedTemplate,String namespace) . | public Template | getTemplateForInclusion(String name, String encoding, boolean parse) Gets a template for inclusion; used with
Environment.include(Template includedTemplate) .
The advantage over simply using config.getTemplate(...) is that it chooses
the default encoding as the include directive does.
Parameters: name - the name of the template, relatively to the template root directory(not the to the directory of the currently executing template file!).(Note that you can use freemarker.cache.TemplateCache.getFullTemplatePathto convert paths to template root relative paths.) Parameters: encoding - the encoding of the obtained template. | TemplateTransformModel | getTransform(Expression exp) | public TemplateModel | getVariable(String name) Returns the variable that is visible in this context. | public Namespace | importLib(String name, String namespace) Emulates import directive, except that name must be tempate
root relative. | public Namespace | importLib(Template loadedTemplate, String namespace) Emulates import directive.
Parameters: loadedTemplate - the template to import. | void | importMacros(Template template) | public void | include(String name, String encoding, boolean parse) Emulates include directive, except that name must be tempate
root relative. | public void | include(Template includedTemplate) Processes a Template in the context of this Environment , including its
output in the Environment 's Writer.
Parameters: includedTemplate - the template to process. | public void | outputInstructionStack(PrintWriter pw) Outputs the instruction stack. | int | parseDateStyleToken(String token) | public void | process() Processes the template to which this environment belongs. | void | recurse(TemplateNodeModel node, TemplateSequenceModel namespaces) | String | renderElementToString(TemplateElement te) | public void | setCurrentVisitorNode(TemplateNodeModel node) sets TemplateNodeModel as the current visitor node. | public void | setDateFormat(String formatName) | public void | setDateTimeFormat(String formatName) | public void | setGlobalVariable(String name, TemplateModel model) Sets a variable that is visible globally. | void | setLastReturnValue(TemplateModel lastReturnValue) | public void | setLocalVariable(String name, TemplateModel model) Sets a local variable (one effective only during a macro invocation). | public void | setLocale(Locale locale) | public void | setNumberFormat(String formatName) | public void | setOut(Writer out) | public void | setOutputEncoding(String outputEncoding) | public void | setTemplateExceptionHandler(TemplateExceptionHandler templateExceptionHandler) | public void | setTimeFormat(String formatName) | public void | setTimeZone(TimeZone timeZone) | public void | setURLEscapingCharset(String urlEscapingCharset) | public void | setVariable(String name, TemplateModel model) Sets a variable in the current namespace. | void | visit(TemplateElement element) "Visit" the template element. | void | visit(TemplateElement element, TemplateTransformModel transform, Map args) | void | visit(TemplateElement attemptBlock, TemplateElement recoveryBlock) | void | visit(BodyInstruction.Context bctxt) | void | visit(IteratorBlock.Context ictxt) | void | visit(TemplateNodeModel node, TemplateSequenceModel namespaces) | void | visit(Macro macro, Map namedArgs, List positionalArgs, List bodyParameterNames, TemplateElement nestedBlock) "visit" a macro. | void | visitMacroDef(Macro macro) |
clearLastReturnValue | void clearLastReturnValue()(Code) | | |
getCurrentEnvironment | public static Environment getCurrentEnvironment()(Code) | | Retrieves the environment object associated with the current
thread. Data model implementations that need access to the
environment can call this method to obtain the environment object
that represents the template processing that is currently running
on the current thread.
|
getCurrentNamespace | public Namespace getCurrentNamespace()(Code) | | Returns the main name-space.
This is correspondent of FTL .namespace hash.
|
getDataModel | public TemplateHashModel getDataModel()(Code) | | Returns the data model hash.
This is correspondent of FTL .datamodel hash.
That is, it contains both the variables of the root hash passed to the
Template.process(...) , and the shared variables in the
Configuration .
|
getDefaultNS | public String getDefaultNS()(Code) | | the default node namespace for the current FTL namespace |
getEffectiveURLEscapingCharset | String getEffectiveURLEscapingCharset()(Code) | | Returns the name of the charset that should be used for URL encoding.
This will be null if the information is not available.
The function caches the return value, so it is quick to call it
repeately.
|
getGlobalNamespace | public Namespace getGlobalNamespace()(Code) | | Returns a fictitious name-space that contains the globally visible variables
that were created in the template, but not the variables of the data-model.
There is no such thing in FTL; this strange method was added because of the
JSP taglib support, since this imaginary name-space contains the page-scope
attributes.
|
getGlobalVariable | public TemplateModel getGlobalVariable(String name) throws TemplateModelException(Code) | | Returns the globally visible variable of the given name (or null).
This is correspondent to FTL .globals.name .
This will first look at variables that were assigned globally via:
<#global ...> and then at the data model exposed to the template.
|
getGlobalVariables | public TemplateHashModel getGlobalVariables()(Code) | | Returns the read-only hash of globally visible variables.
This is the correspondent of FTL .globals hash.
That is, you see the variables created with
<#global ...> , and the variables of the data-model.
To create new global variables, use
Environment.setGlobalVariable setGlobalVariable .
|
getKnownVariableNames | public Set getKnownVariableNames() throws TemplateModelException(Code) | | Returns a set of variable names that are known at the time of call. This
includes names of all shared variables in the
Configuration ,
names of all global variables that were assigned during the template processing,
names of all variables in the current name-space, names of all local variables
and loop variables. If the passed root data model implements the
TemplateHashModelEx interface, then all names it retrieves through a call to
TemplateHashModelEx.keys method are returned as well.
The method returns a new Set object on each call that is completely
disconnected from the Environment. That is, modifying the set will have
no effect on the Environment object.
|
getLocalVariable | public TemplateModel getLocalVariable(String name) throws TemplateModelException(Code) | | Returns the loop or macro local variable corresponding to this
variable name. Possibly null.
(Note that the misnomer is kept for backward compatibility: loop variables
are not local variables according to our terminology.)
|
getMacroNamespace | Namespace getMacroNamespace(Macro macro)(Code) | | |
getMainNamespace | public Namespace getMainNamespace()(Code) | | Returns the main name-space.
This is correspondent of FTL .main hash.
|
getNamespace | public Namespace getNamespace(String name)(Code) | | Returns the name-space for the name if exists, or null.
Parameters: name - the template path that you have used with the import directiveor Environment.importLib(String,String) call, in normalized form. That is, the path must be an absolutepath, and it must not contain "/../" or "/./". The leading "/" is optional. |
getNamespaceForPrefix | public String getNamespaceForPrefix(String prefix)(Code) | | the namespace URI registered for this prefix, or null.This is based on the mappings registered in the current namespace. |
getTemplate | public Template getTemplate()(Code) | | Retrieves the currently processed template.
|
getTemplateForInclusion | public Template getTemplateForInclusion(String name, String encoding, boolean parse) throws IOException(Code) | | Gets a template for inclusion; used with
Environment.include(Template includedTemplate) .
The advantage over simply using config.getTemplate(...) is that it chooses
the default encoding as the include directive does.
Parameters: name - the name of the template, relatively to the template root directory(not the to the directory of the currently executing template file!).(Note that you can use freemarker.cache.TemplateCache.getFullTemplatePathto convert paths to template root relative paths.) Parameters: encoding - the encoding of the obtained template. If null,the encoding of the Template that is currently being processed in thisEnvironment is used. Parameters: parse - whether to process a parsed template or just include theunparsed template source. |
getVariable | public TemplateModel getVariable(String name) throws TemplateModelException(Code) | | Returns the variable that is visible in this context.
This is the correspondent to an FTL top-level variable reading expression.
That is, it tries to find the the variable in this order:
- An loop variable (if we're in a loop or user defined directive body) such as foo_has_next
- A local variable (if we're in a macro)
- A variable defined in the current namespace (say, via <#assign ...>)
- A variable defined globally (say, via <#global ....>)
- Variable in the data model:
- A variable in the root hash that was exposed to this
rendering environment in the Template.process(...) call
- A shared variable set in the configuration via a call to Configuration.setSharedVariable(...)
|
include | public void include(String name, String encoding, boolean parse) throws IOException, TemplateException(Code) | | Emulates include directive, except that name must be tempate
root relative.
It's the same as include(getTemplateForInclusion(name, encoding, parse)) .
But, you may want to separately call these two methods, so you can determine the source of
exceptions more precisely, and thus achieve more intelligent error handling.
See Also: Environment.getTemplateForInclusion(String name,String encoding,boolean parse) See Also: Environment.include(Template includedTemplate) |
outputInstructionStack | public void outputInstructionStack(PrintWriter pw)(Code) | | Outputs the instruction stack. Useful for debugging.
TemplateException s incorporate this information in their stack
traces.
|
parseDateStyleToken | int parseDateStyleToken(String token)(Code) | | |
setCurrentVisitorNode | public void setCurrentVisitorNode(TemplateNodeModel node)(Code) | | sets TemplateNodeModel as the current visitor node. .current_node
|
setDateFormat | public void setDateFormat(String formatName)(Code) | | |
setDateTimeFormat | public void setDateTimeFormat(String formatName)(Code) | | |
setGlobalVariable | public void setGlobalVariable(String name, TemplateModel model)(Code) | | Sets a variable that is visible globally.
This is correspondent to FTL <#global name=model> .
This can be considered a convenient shorthand for:
getGlobalNamespace().put(name, model)
|
setLocalVariable | public void setLocalVariable(String name, TemplateModel model)(Code) | | Sets a local variable (one effective only during a macro invocation).
This is correspondent to FTL <#local name=model> .
Parameters: name - the identifier of the variable Parameters: model - the value of the variable. throws: IllegalStateException - if the environment is not executing amacro body. |
setNumberFormat | public void setNumberFormat(String formatName)(Code) | | |
setOutputEncoding | public void setOutputEncoding(String outputEncoding)(Code) | | |
setTimeFormat | public void setTimeFormat(String formatName)(Code) | | |
setURLEscapingCharset | public void setURLEscapingCharset(String urlEscapingCharset)(Code) | | |
setVariable | public void setVariable(String name, TemplateModel model)(Code) | | Sets a variable in the current namespace.
This is correspondent to FTL <#assign name=model> .
This can be considered a convenient shorthand for:
getCurrentNamespace().put(name, model)
|
visit | void visit(TemplateElement element, TemplateTransformModel transform, Map args) throws TemplateException, IOException(Code) | | "Visit" the template element, passing the output
through a TemplateTransformModel
Parameters: element - the element to visit through a transform Parameters: transform - the transform to pass the element outputthrough Parameters: args - optional arguments fed to the transform |
|
|