public final class Environment extends Configurable
Data models that need to access the Environment
object that represents the template processing on the current thread can use
the getCurrentEnvironment() method.
If you need to modify or read this object before or after the process
call, use Template.createProcessingEnvironment(Object rootMap, Writer out, ObjectWrapper wrapper)
| Modifier and Type | Class and Description |
|---|---|
class |
Environment.Namespace |
Configurable.UnknownSettingExceptionARITHMETIC_ENGINE_KEY, AUTO_FLUSH_KEY, BOOLEAN_FORMAT_KEY, CLASSIC_COMPATIBLE_KEY, DATE_FORMAT_KEY, DATETIME_FORMAT_KEY, LOCALE_KEY, NEW_BUILTIN_CLASS_RESOLVER_KEY, NUMBER_FORMAT_KEY, OBJECT_WRAPPER_KEY, OUTPUT_ENCODING_KEY, STRICT_BEAN_MODELS, TEMPLATE_EXCEPTION_HANDLER_KEY, TIME_FORMAT_KEY, TIME_ZONE_KEY, URL_ESCAPING_CHARSET_KEY| Constructor and Description |
|---|
Environment(Template template,
TemplateHashModel rootDataModel,
java.io.Writer out) |
| Modifier and Type | Method and Description |
|---|---|
java.lang.Object |
__getitem__(java.lang.String key)
A hook that Jython uses.
|
void |
__setitem__(java.lang.String key,
java.lang.Object o)
A hook that Jython uses.
|
boolean |
applyEqualsOperator(TemplateModel leftValue,
TemplateModel rightValue)
Compares two
TemplateModel-s according the rules of the FTL "==" operator. |
boolean |
applyEqualsOperatorLenient(TemplateModel leftValue,
TemplateModel rightValue)
Compares two
TemplateModel-s according the rules of the FTL "==" operator, except that if the two types
are incompatible, they are treated as non-equal instead of throwing an exception. |
boolean |
applyGreaterThanOperator(TemplateModel leftValue,
TemplateModel rightValue)
Compares two
TemplateModel-s according the rules of the FTL ">" operator. |
boolean |
applyLessThanOperator(TemplateModel leftValue,
TemplateModel rightValue)
Compares two
TemplateModel-s according the rules of the FTL "<" operator. |
boolean |
applyLessThanOrEqualsOperator(TemplateModel leftValue,
TemplateModel rightValue)
Compares two
TemplateModel-s according the rules of the FTL "<" operator. |
boolean |
applyWithGreaterThanOrEqualsOperator(TemplateModel leftValue,
TemplateModel rightValue)
Compares two
TemplateModel-s according the rules of the FTL ">=" operator. |
java.text.NumberFormat |
getCNumberFormat()
Returns the
NumberFormat used for the c built-in. |
Configuration |
getConfiguration() |
static Environment |
getCurrentEnvironment()
Retrieves the environment object associated with the current
thread.
|
Environment.Namespace |
getCurrentNamespace()
Returns the main name-space.
|
TemplateNodeModel |
getCurrentVisitorNode() |
TemplateHashModel |
getDataModel() |
java.lang.String |
getDefaultNS() |
Environment.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.
|
TemplateModel |
getGlobalVariable(java.lang.String name)
Returns the globally visible variable of the given name (or null).
|
TemplateHashModel |
getGlobalVariables()
Returns the read-only hash of globally visible variables.
|
java.util.Set |
getKnownVariableNames()
Returns a set of variable names that are known at the time of call.
|
TemplateModel |
getLocalVariable(java.lang.String name)
Returns the loop or macro local variable corresponding to this
variable name.
|
Environment.Namespace |
getMainNamespace()
Returns the main name-space.
|
Environment.Namespace |
getNamespace(java.lang.String name)
Returns the name-space for the name if exists, or null.
|
java.lang.String |
getNamespaceForPrefix(java.lang.String prefix) |
java.io.Writer |
getOut() |
java.lang.String |
getPrefixForNamespace(java.lang.String nsURI) |
Template |
getTemplate()
Retrieves the currently processed template.
|
Template |
getTemplateForImporting(java.lang.String name)
Gets a template for importing; used with
importLib(Template importedTemplate, String namespace). |
Template |
getTemplateForInclusion(java.lang.String name,
java.lang.String encoding,
boolean parse)
Gets a template for inclusion; used with
include(Template includedTemplate). |
TemplateModel |
getVariable(java.lang.String name)
Returns the variable that is visible in this context.
|
Environment.Namespace |
importLib(java.lang.String name,
java.lang.String namespace)
Emulates
import directive, except that name must be tempate
root relative. |
Environment.Namespace |
importLib(Template loadedTemplate,
java.lang.String namespace)
Emulates
import directive. |
void |
include(java.lang.String name,
java.lang.String encoding,
boolean parse)
Emulates
include directive, except that name must be tempate
root relative. |
void |
include(Template includedTemplate)
Processes a Template in the context of this
Environment, including its
output in the Environment's Writer. |
boolean |
isInAttemptBlock()
Tells if we are inside an #attempt block (but before #recover).
|
void |
outputInstructionStack(java.io.PrintWriter pw)
Prints the current FTL stack trace.
|
void |
process()
Processes the template to which this environment belongs.
|
void |
setCurrentVisitorNode(TemplateNodeModel node)
sets TemplateNodeModel as the current visitor node.
|
void |
setDateFormat(java.lang.String formatName)
Sets the format used to convert
Date-s to string-s that are date-only (no time part) values. |
void |
setDateTimeFormat(java.lang.String formatName)
Sets the format used to convert
Date-s to string-s that are date+time values. |
void |
setGlobalVariable(java.lang.String name,
TemplateModel model)
Sets a variable that is visible globally.
|
void |
setLocale(java.util.Locale locale)
Sets the default locale used for number and date formatting (among others), also the locale used for searching
localized template variations when no locale was explicitly requested.
|
void |
setLocalVariable(java.lang.String name,
TemplateModel model)
Sets a local variable (one effective only during a macro invocation).
|
void |
setNumberFormat(java.lang.String formatName)
Sets the number format used to convert numbers to strings.
|
void |
setOut(java.io.Writer out) |
void |
setOutputEncoding(java.lang.String outputEncoding)
Informs FreeMarker about the charset used for the output.
|
void |
setTemplateExceptionHandler(TemplateExceptionHandler templateExceptionHandler)
Sets the exception handler used to handle exceptions occurring inside templates.
|
void |
setTimeFormat(java.lang.String formatName)
Sets the format used to convert
Date-s to string-s that are time-only (not date part) values. |
void |
setTimeZone(java.util.TimeZone timeZone)
Sets the time zone to use when formatting date/time values.
|
void |
setURLEscapingCharset(java.lang.String urlEscapingCharset)
Sets the URL escaping charset.
|
void |
setVariable(java.lang.String name,
TemplateModel model)
Sets a variable in the current namespace.
|
void |
visit(freemarker.core.TemplateElement element,
TemplateDirectiveModel directiveModel,
java.util.Map args,
java.util.List bodyParameterNames) |
clone, doAutoImportsAndIncludes, getArithmeticEngine, getAutoFlush, getBooleanFormat, getClassicCompatibleAsInt, getCustomAttribute, getCustomAttributeNames, getDateFormat, getDateTimeFormat, getEnvironment, getLocale, getNewBuiltinClassResolver, getNumberFormat, getObjectWrapper, getOutputEncoding, getParent, getSetting, getSettings, getTemplateExceptionHandler, getTimeFormat, getTimeZone, getURLEscapingCharset, invalidSettingValueException, isClassicCompatible, parseAsImportList, parseAsList, parseAsSegmentedList, removeCustomAttribute, setArithmeticEngine, setAutoFlush, setBooleanFormat, setClassicCompatible, setClassicCompatibleAsInt, setCustomAttribute, setNewBuiltinClassResolver, setObjectWrapper, setSetting, setSettings, setSettings, setStrictBeanModels, unknownSettingExceptionpublic Environment(Template template, TemplateHashModel rootDataModel, java.io.Writer out)
public static Environment getCurrentEnvironment()
public Template getTemplate()
public void process()
throws TemplateException,
java.io.IOException
TemplateExceptionjava.io.IOExceptionpublic void visit(freemarker.core.TemplateElement element,
TemplateDirectiveModel directiveModel,
java.util.Map args,
java.util.List bodyParameterNames)
throws TemplateException,
java.io.IOException
TemplateExceptionjava.io.IOExceptionpublic boolean isInAttemptBlock()
TemplateExceptionHandler-s, as then they may don't want to print the error to the output, as
#attempt will roll it back anyway.public void setTemplateExceptionHandler(TemplateExceptionHandler templateExceptionHandler)
ConfigurableTemplateExceptionHandler.DEBUG_HANDLER. The recommended values are:
TemplateExceptionHandler.RETHROW_HANDLER
TemplateExceptionHandler.HTML_DEBUG_HANDLER
TemplateExceptionHandler.DEBUG_HANDLER
All of these will let the exception propagate further, so that you can catch it around
Template.process(Object, Writer) for example. The difference is in what they print on the output before
they do that.
Note that the TemplateExceptionHandler is not meant to be used for generating HTTP error pages.
Neither is it meant to be used to roll back the printed output. These should be solved outside template
processing when the exception raises from Template.process.
TemplateExceptionHandler meant to be used if you want to include special content in the template
output, or if you want to suppress certain exceptions.
setTemplateExceptionHandler in class Configurablepublic void setLocale(java.util.Locale locale)
ConfigurablesetLocale in class ConfigurableConfiguration.getTemplate(String, Locale)public void setTimeZone(java.util.TimeZone timeZone)
ConfigurableEnvironment to match the preferred time zone of the visitor.setTimeZone in class Configurablepublic void setURLEscapingCharset(java.lang.String urlEscapingCharset)
Configurablenull), the output encoding
(Configurable.setOutputEncoding(String)) will be used for URL escaping.
Defaults to null.setURLEscapingCharset in class Configurablepublic void setOutputEncoding(java.lang.String outputEncoding)
Configurablenull means that the output encoding is not known.
Defaults to null (unknown).
setOutputEncoding in class Configurablepublic boolean applyEqualsOperator(TemplateModel leftValue, TemplateModel rightValue) throws TemplateException
TemplateModel-s according the rules of the FTL "==" operator.TemplateExceptionpublic boolean applyEqualsOperatorLenient(TemplateModel leftValue, TemplateModel rightValue) throws TemplateException
TemplateModel-s according the rules of the FTL "==" operator, except that if the two types
are incompatible, they are treated as non-equal instead of throwing an exception. Comparing dates of
different types (date-only VS time-only VS date-time) will still throw an exception, however.TemplateExceptionpublic boolean applyLessThanOperator(TemplateModel leftValue, TemplateModel rightValue) throws TemplateException
TemplateModel-s according the rules of the FTL "<" operator.TemplateExceptionpublic boolean applyLessThanOrEqualsOperator(TemplateModel leftValue, TemplateModel rightValue) throws TemplateException
TemplateModel-s according the rules of the FTL "<" operator.TemplateExceptionpublic boolean applyGreaterThanOperator(TemplateModel leftValue, TemplateModel rightValue) throws TemplateException
TemplateModel-s according the rules of the FTL ">" operator.TemplateExceptionpublic boolean applyWithGreaterThanOrEqualsOperator(TemplateModel leftValue, TemplateModel rightValue) throws TemplateException
TemplateModel-s according the rules of the FTL ">=" operator.TemplateExceptionpublic void setOut(java.io.Writer out)
public java.io.Writer getOut()
public void setNumberFormat(java.lang.String formatName)
ConfigurablesetNumberFormat in class Configurablepublic void setTimeFormat(java.lang.String formatName)
ConfigurableDate-s to string-s that are time-only (not date part) values.
Possible values are patterns accepted by Java's DateFormat, also "short",
"medium", "long" and "full" that has locale-dependent meaning also defined by
DateFormat.
Defaults to "", which means "use the FreeMarker default", which is currently "medium".
setTimeFormat in class Configurablepublic void setDateFormat(java.lang.String formatName)
ConfigurableDate-s to string-s that are date-only (no time part) values.
Possible values are patterns accepted by Java's DateFormat, also "short",
"medium", "long" and "full" that has locale-dependent meaning also defined by
DateFormat.
Defaults to "", which means "use the FreeMarker default", which is currently "medium".
setDateFormat in class Configurablepublic void setDateTimeFormat(java.lang.String formatName)
ConfigurableDate-s to string-s that are date+time values.
Possible values are patterns accepted by Java's DateFormat, also "short",
"medium", "long" and "full" that has locale-dependent meaning also defined by
DateFormat.
It's also possible to give values like "short_long" (in any combinations), which will
use "short" for the date part, and "long" for the time part.
Defaults to "", which means "use the FreeMarker default", which is currently "medium".
setDateTimeFormat in class Configurablepublic Configuration getConfiguration()
public java.text.NumberFormat getCNumberFormat()
NumberFormat used for the c built-in.
This is always US English "0.################", without
grouping and without superfluous decimal separator.public TemplateModel getLocalVariable(java.lang.String name) throws TemplateModelException
TemplateModelExceptionpublic TemplateModel getVariable(java.lang.String name) throws TemplateModelException
TemplateModelExceptionpublic TemplateModel getGlobalVariable(java.lang.String name) throws TemplateModelException
.globals.name.
This will first look at variables that were assigned globally via:
<#global ...> and then at the data model exposed to the template.TemplateModelExceptionpublic void setGlobalVariable(java.lang.String name,
TemplateModel model)
<#global name=model>.
This can be considered a convenient shorthand for:
getGlobalNamespace().put(name, model)public void setVariable(java.lang.String name,
TemplateModel model)
<#assign name=model>.
This can be considered a convenient shorthand for:
getCurrentNamespace().put(name, model)public void setLocalVariable(java.lang.String name,
TemplateModel model)
<#local name=model>.name - the identifier of the variablemodel - the value of the variable.java.lang.IllegalStateException - if the environment is not executing a
macro body.public java.util.Set getKnownVariableNames()
throws TemplateModelException
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.TemplateModelExceptionpublic void outputInstructionStack(java.io.PrintWriter pw)
TemplateExceptions incorporate this information in their stack traces.public Environment.Namespace getNamespace(java.lang.String name)
name - the template path that you have used with the import directive
or importLib(String, String) call, in normalized form. That is, the path must be an absolute
path, and it must not contain "/../" or "/./". The leading "/" is optional.public Environment.Namespace getMainNamespace()
.main hash.public Environment.Namespace getCurrentNamespace()
.namespace hash.public Environment.Namespace getGlobalNamespace()
public TemplateHashModel getDataModel()
public TemplateHashModel getGlobalVariables()
.globals hash.
That is, you see the variables created with
<#global ...>, and the variables of the data-model.
To create new global variables, use setGlobalVariable.public TemplateNodeModel getCurrentVisitorNode()
public void setCurrentVisitorNode(TemplateNodeModel node)
public void include(java.lang.String name,
java.lang.String encoding,
boolean parse)
throws java.io.IOException,
TemplateException
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.
java.io.IOExceptionTemplateExceptiongetTemplateForInclusion(String name, String encoding, boolean parse),
include(Template includedTemplate)public Template getTemplateForInclusion(java.lang.String name, java.lang.String encoding, boolean parse) throws java.io.IOException
include(Template includedTemplate).
The advantage over simply using config.getTemplate(...) is that it chooses
the default encoding as the include directive does.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 TemplateCache.getFullTemplatePath(freemarker.core.Environment, java.lang.String, java.lang.String)
to convert paths to template root relative paths.)encoding - the encoding of the obtained template. If null,
the encoding of the Template that is currently being processed in this
Environment is used.parse - whether to process a parsed template or just include the
unparsed template source.java.io.IOExceptionpublic void include(Template includedTemplate) throws TemplateException, java.io.IOException
Environment, including its
output in the Environment's Writer.includedTemplate - the template to process. Note that it does not need
to be a template returned by
getTemplateForInclusion(String name, String encoding, boolean parse).TemplateExceptionjava.io.IOExceptionpublic Environment.Namespace importLib(java.lang.String name, java.lang.String namespace) throws java.io.IOException, TemplateException
import directive, except that name must be tempate
root relative.
It's the same as importLib(getTemplateForImporting(name), namespace).
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.
java.io.IOExceptionTemplateExceptiongetTemplateForImporting(String name),
importLib(Template includedTemplate, String namespace)public Template getTemplateForImporting(java.lang.String name) throws java.io.IOException
importLib(Template importedTemplate, String namespace). The advantage
over simply using config.getTemplate(...) is that it chooses the encoding
as the import directive does.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 TemplateCache.getFullTemplatePath(freemarker.core.Environment, java.lang.String, java.lang.String)
to convert paths to template root relative paths.)java.io.IOExceptionpublic Environment.Namespace importLib(Template loadedTemplate, java.lang.String namespace) throws java.io.IOException, TemplateException
import directive.loadedTemplate - the template to import. Note that it does not need
to be a template returned by getTemplateForImporting(String name).java.io.IOExceptionTemplateExceptionpublic java.lang.String getNamespaceForPrefix(java.lang.String prefix)
public java.lang.String getPrefixForNamespace(java.lang.String nsURI)
public java.lang.String getDefaultNS()
public java.lang.Object __getitem__(java.lang.String key)
throws TemplateModelException
TemplateModelExceptionpublic void __setitem__(java.lang.String key,
java.lang.Object o)
throws TemplateException
TemplateException