Class Environment
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 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)
-
Nested Class Summary
Nested classes/interfaces inherited from class freemarker.core.Configurable
Configurable.SettingValueAssignmentException, Configurable.UnknownSettingException
-
Field Summary
Fields inherited from class freemarker.core.Configurable
API_BUILTIN_ENABLED_KEY, API_BUILTIN_ENABLED_KEY_CAMEL_CASE, API_BUILTIN_ENABLED_KEY_SNAKE_CASE, ARITHMETIC_ENGINE_KEY, ARITHMETIC_ENGINE_KEY_CAMEL_CASE, ARITHMETIC_ENGINE_KEY_SNAKE_CASE, ATTEMPT_EXCEPTION_REPORTER_KEY, ATTEMPT_EXCEPTION_REPORTER_KEY_CAMEL_CASE, ATTEMPT_EXCEPTION_REPORTER_KEY_SNAKE_CASE, AUTO_FLUSH_KEY, AUTO_FLUSH_KEY_CAMEL_CASE, AUTO_FLUSH_KEY_SNAKE_CASE, AUTO_IMPORT_KEY, AUTO_IMPORT_KEY_CAMEL_CASE, AUTO_IMPORT_KEY_SNAKE_CASE, AUTO_INCLUDE_KEY, AUTO_INCLUDE_KEY_CAMEL_CASE, AUTO_INCLUDE_KEY_SNAKE_CASE, BOOLEAN_FORMAT_KEY, BOOLEAN_FORMAT_KEY_CAMEL_CASE, BOOLEAN_FORMAT_KEY_SNAKE_CASE, C_FORMAT_KEY, C_FORMAT_KEY_CAMEL_CASE, C_FORMAT_KEY_SNAKE_CASE, CLASSIC_COMPATIBLE_KEY, CLASSIC_COMPATIBLE_KEY_CAMEL_CASE, CLASSIC_COMPATIBLE_KEY_SNAKE_CASE, CUSTOM_DATE_FORMATS_KEY, CUSTOM_DATE_FORMATS_KEY_CAMEL_CASE, CUSTOM_DATE_FORMATS_KEY_SNAKE_CASE, CUSTOM_NUMBER_FORMATS_KEY, CUSTOM_NUMBER_FORMATS_KEY_CAMEL_CASE, CUSTOM_NUMBER_FORMATS_KEY_SNAKE_CASE, DATE_FORMAT_KEY, DATE_FORMAT_KEY_CAMEL_CASE, DATE_FORMAT_KEY_SNAKE_CASE, DATETIME_FORMAT_KEY, DATETIME_FORMAT_KEY_CAMEL_CASE, DATETIME_FORMAT_KEY_SNAKE_CASE, LAZY_AUTO_IMPORTS_KEY, LAZY_AUTO_IMPORTS_KEY_CAMEL_CASE, LAZY_AUTO_IMPORTS_KEY_SNAKE_CASE, LAZY_IMPORTS_KEY, LAZY_IMPORTS_KEY_CAMEL_CASE, LAZY_IMPORTS_KEY_SNAKE_CASE, LOCALE_KEY, LOCALE_KEY_CAMEL_CASE, LOCALE_KEY_SNAKE_CASE, LOG_TEMPLATE_EXCEPTIONS_KEY, LOG_TEMPLATE_EXCEPTIONS_KEY_CAMEL_CASE, LOG_TEMPLATE_EXCEPTIONS_KEY_SNAKE_CASE, NEW_BUILTIN_CLASS_RESOLVER_KEY, NEW_BUILTIN_CLASS_RESOLVER_KEY_CAMEL_CASE, NEW_BUILTIN_CLASS_RESOLVER_KEY_SNAKE_CASE, NUMBER_FORMAT_KEY, NUMBER_FORMAT_KEY_CAMEL_CASE, NUMBER_FORMAT_KEY_SNAKE_CASE, OBJECT_WRAPPER_KEY, OBJECT_WRAPPER_KEY_CAMEL_CASE, OBJECT_WRAPPER_KEY_SNAKE_CASE, OUTPUT_ENCODING_KEY, OUTPUT_ENCODING_KEY_CAMEL_CASE, OUTPUT_ENCODING_KEY_SNAKE_CASE, SHOW_ERROR_TIPS_KEY, SHOW_ERROR_TIPS_KEY_CAMEL_CASE, SHOW_ERROR_TIPS_KEY_SNAKE_CASE, SQL_DATE_AND_TIME_TIME_ZONE_KEY, SQL_DATE_AND_TIME_TIME_ZONE_KEY_CAMEL_CASE, SQL_DATE_AND_TIME_TIME_ZONE_KEY_SNAKE_CASE, STRICT_BEAN_MODELS, STRICT_BEAN_MODELS_KEY, STRICT_BEAN_MODELS_KEY_CAMEL_CASE, STRICT_BEAN_MODELS_KEY_SNAKE_CASE, TEMPLATE_EXCEPTION_HANDLER_KEY, TEMPLATE_EXCEPTION_HANDLER_KEY_CAMEL_CASE, TEMPLATE_EXCEPTION_HANDLER_KEY_SNAKE_CASE, TIME_FORMAT_KEY, TIME_FORMAT_KEY_CAMEL_CASE, TIME_FORMAT_KEY_SNAKE_CASE, TIME_ZONE_KEY, TIME_ZONE_KEY_CAMEL_CASE, TIME_ZONE_KEY_SNAKE_CASE, TRUNCATE_BUILTIN_ALGORITHM_KEY, TRUNCATE_BUILTIN_ALGORITHM_KEY_CAMEL_CASE, TRUNCATE_BUILTIN_ALGORITHM_KEY_SNAKE_CASE, URL_ESCAPING_CHARSET_KEY, URL_ESCAPING_CHARSET_KEY_CAMEL_CASE, URL_ESCAPING_CHARSET_KEY_SNAKE_CASE, WRAP_UNCHECKED_EXCEPTIONS_KEY, WRAP_UNCHECKED_EXCEPTIONS_KEY_CAMEL_CASE, WRAP_UNCHECKED_EXCEPTIONS_KEY_SNAKE_CASE
-
Constructor Summary
ConstructorDescriptionEnvironment(Template template, TemplateHashModel rootDataModel, Writer out)
Creates an environment with the given main (top-level) template that it intends to process() later; typically, it's better to useTemplate.createProcessingEnvironment(Object, Writer)
instead of this. -
Method Summary
Modifier and TypeMethodDescription__getitem__(String key)
A hook that Jython uses.void
__setitem__(String key, Object o)
A hook that Jython uses.boolean
applyEqualsOperator(TemplateModel leftValue, TemplateModel rightValue)
Compares twoTemplateModel
-s according the rules of the FTL "==" operator.boolean
applyEqualsOperatorLenient(TemplateModel leftValue, TemplateModel rightValue)
Compares twoTemplateModel
-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 twoTemplateModel
-s according the rules of the FTL ">" operator.boolean
applyLessThanOperator(TemplateModel leftValue, TemplateModel rightValue)
Compares twoTemplateModel
-s according the rules of the FTL "<" operator.boolean
applyLessThanOrEqualsOperator(TemplateModel leftValue, TemplateModel rightValue)
Compares twoTemplateModel
-s according the rules of the FTL "<" operator.boolean
applyWithGreaterThanOrEqualsOperator(TemplateModel leftValue, TemplateModel rightValue)
Compares twoTemplateModel
-s according the rules of the FTL ">=" operator.Deprecated.Gets the currently executing custom directive's call place information, ornull
if there's no executing custom directive.static Environment
Retrieves the environment object associated with the current thread, ornull
if there's no template processing going on in this thread.Returns the current namespace.Returns theTemplate
that we are "lexically" inside at the moment.getCustomState(Object identityKey)
Returns the value of a custom state variable, ornull
if it's missing; seesetCustomState(Object, Object)
for more.Returns a view of the data-model (also known as the template context in some other template engines) that falls back to shared variables.Returns the variable from the data-model, or if it's not there, then from the Configuration.setSharedVariables(Map) shared variables}Returns the name-space that contains the globally visible non-data-model variables (usually created with<#global ...>
).getGlobalVariable(String name)
Returns the globally visible variable of the given name, ornull
.Returns the read-only hash of globally visible variables.Returns a set of variable names that are known at the time of call.getLocalVariable(String name)
Returns the loop or macro local variable corresponding to this variable name.Returns the main namespace.Returns the topmostTemplate
, with other words, the one for which thisEnvironment
was created.getNamespace(String name)
Returns the name-space for the name if exists, or null.getNamespaceForPrefix(String prefix)
getOut()
getPrefixForNamespace(String nsURI)
Deprecated.UsegetMainTemplate()
instead (orgetCurrentNamespace()
and thenEnvironment.Namespace.getTemplate()
); the value returned by this method is often not what you expect when it comes to macro/function invocations.getTemplateDateFormat(int dateType, Class<? extends Date> dateClass)
Gets aTemplateDateFormat
using the date/time/datetime format settings and the current locale and time zone.getTemplateDateFormat(String formatString, int dateType, Class<? extends Date> dateClass)
Gets aTemplateDateFormat
for the specified format string and the current locale and time zone.getTemplateDateFormat(String formatString, int dateType, Class<? extends Date> dateClass, Locale locale)
LikegetTemplateDateFormat(String, int, Class)
, but allows you to use a different locale than the current one.getTemplateDateFormat(String formatString, int dateType, Class<? extends Date> dateClass, Locale locale, TimeZone timeZone, TimeZone sqlDateAndTimeTimeZone)
LikegetTemplateDateFormat(String, int, Class)
, but allows you to use a different locale and time zone than the current one.getTemplateDateFormat(String formatString, int dateType, Locale locale, TimeZone timeZone, boolean zonelessInput)
Gets aTemplateDateFormat
for the specified parameters.getTemplateForImporting(String name)
Gets a template for importing; used withimportLib(Template importedTemplate, String namespace)
.getTemplateForInclusion(String name, String encoding, boolean parse)
Same asgetTemplateForInclusion(String, String, boolean, boolean)
withfalse
ignoreMissing
argument.getTemplateForInclusion(String name, String encoding, boolean parseAsFTL, boolean ignoreMissing)
Gets a template for inclusion; used for implementinginclude(Template includedTemplate)
.Returns the current number format (Configurable.getNumberFormat()
) asTemplateNumberFormat
.getTemplateNumberFormat(String formatString)
Returns the number format asTemplateNumberFormat
for the given format string and the current locale.getTemplateNumberFormat(String formatString, Locale locale)
Returns the number format asTemplateNumberFormat
, for the given format string and locale.Getter pair ofsetTemplateProcessingTracer(TemplateProcessingTracer)
.getVariable(String name)
Returns the variable that is visible in this context, ornull
if the variable is not found.Does what the#import
directive does, but with an already loaded template.Emulatesimport
directive, except thattemplateName
must be template root relative.LikeimportLib(String, String)
, but you can specify if you want a lazy import or not.void
Processes a Template in the context of thisEnvironment
, including its output in theEnvironment
's Writer.void
Emulatesinclude
directive, except thatname
must be template root relative.boolean
Tells if we are inside an#attempt
block (but before#recover
).void
Prints the current FTL stack trace.void
process()
Processes the template to which this environment belongs to.rootBasedToAbsoluteTemplateName(String rootBasedName)
Converts a root based name (a name that's either relative to the root, or is absolute), which are typically used by the API (such as forConfiguration.getTemplate(String)
), to an absolute name, which can be safely passed to<#include path>
and such, as it won't be misinterpreted to be relative to the directory of the template.void
setBooleanFormat(String booleanFormat)
The string value for the booleantrue
andfalse
values, usually intended for human consumption (not for a computer language), separated with comma.void
setCFormat(CFormat cFormat)
Sets the format (usually a computer language) used for?c
,?cn
, and for the"c"
("computer"
before 2.3.32)number_format
, and the"c"
boolean_format
.void
sets TemplateNodeModel as the current visitor node.setCustomState(Object identityKey, Object value)
Sets the value of a custom state variable.void
setDateFormat(String dateFormat)
Sets the format used to convertDate
-s that are date-only (no time part) values to string-s, also the format thatsomeString?date
will use to parse strings.void
setDateTimeFormat(String dateTimeFormat)
Sets the format used to convertDate
-s that are date-time (timestamp) values to string-s, also the format thatsomeString?datetime
will use to parse strings.void
setGlobalVariable(String name, TemplateModel value)
Sets a variable in the global namespace, like<#global name=value>
.void
Sets the 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(String name, TemplateModel value)
Sets a local variable that's on the top-level inside a macro or function invocation, like<#local name=value>
.void
setNumberFormat(String formatName)
Sets the number format used to convert numbers to strings.void
void
setOutputEncoding(String outputEncoding)
Informs FreeMarker about the charset used for the output.void
setSQLDateAndTimeTimeZone(TimeZone timeZone)
Sets the time zone used when dealing withjava.sql.Date
andjava.sql.Time
values.void
setTemplateExceptionHandler(TemplateExceptionHandler templateExceptionHandler)
Sets the exception handler used to handle exceptions occurring inside templates.void
setTemplateProcessingTracer(TemplateProcessingTracer templateProcessingTracer)
void
setTimeFormat(String timeFormat)
Sets the format used to convertDate
-s that are time (no date part) values to string-s, also the format thatsomeString?time
will use to parse strings.void
setTimeZone(TimeZone timeZone)
Sets the time zone to use when formatting date/time values.void
setURLEscapingCharset(String urlEscapingCharset)
Sets the URL escaping (URL encoding, percentage encoding) charset.void
setVariable(String name, TemplateModel value)
Sets a variable in the current namespace, like<#assign name=value>
.toFullTemplateName(String baseName, String targetName)
Resolves a reference to a template (like the one used in#include
or#import
), assuming a base name.void
visit(freemarker.core.TemplateElement element, TemplateDirectiveModel directiveModel, Map args, List bodyParameterNames)
Deprecated.Should be internal APIMethods inherited from class freemarker.core.Configurable
addAutoImport, addAutoInclude, clone, doAutoImportsAndIncludes, getArithmeticEngine, getAttemptExceptionReporter, getAutoFlush, getAutoImports, getAutoImportsWithoutFallback, getAutoIncludes, getAutoIncludesWithoutFallback, getBooleanFormat, getCFormat, getClassicCompatibleAsInt, getCorrectedNameForUnknownSetting, getCustomAttribute, getCustomAttributeNames, getCustomDateFormat, getCustomDateFormats, getCustomDateFormatsWithoutFallback, getCustomNumberFormat, getCustomNumberFormats, getCustomNumberFormatsWithoutFallback, getDateFormat, getDateTimeFormat, getEnvironment, getLazyAutoImports, getLazyImports, getLocale, getLogTemplateExceptions, getNewBuiltinClassResolver, getNumberFormat, getObjectWrapper, getOutputEncoding, getParent, getSetting, getSettingNames, getSettings, getShowErrorTips, getSQLDateAndTimeTimeZone, getTemplateExceptionHandler, getTimeFormat, getTimeZone, getTruncateBuiltinAlgorithm, getURLEscapingCharset, getWrapUncheckedExceptions, hasCustomFormats, invalidSettingValueException, isAPIBuiltinEnabled, isAPIBuiltinEnabledSet, isArithmeticEngineSet, isAttemptExceptionReporterSet, isAutoFlushSet, isAutoImportsSet, isAutoIncludesSet, isBooleanFormatSet, isCFormatSet, isClassicCompatible, isClassicCompatibleSet, isCustomDateFormatsSet, isCustomNumberFormatsSet, isDateFormatSet, isDateTimeFormatSet, isLazyAutoImportsSet, isLazyImportsSet, isLocaleSet, isLogTemplateExceptionsSet, isNewBuiltinClassResolverSet, isNumberFormatSet, isObjectWrapperSet, isOutputEncodingSet, isShowErrorTipsSet, isSQLDateAndTimeTimeZoneSet, isTemplateExceptionHandlerSet, isTimeFormatSet, isTimeZoneSet, isTruncateBuiltinAlgorithmSet, isURLEscapingCharsetSet, isWrapUncheckedExceptionsSet, parseAsImportList, parseAsList, parseAsSegmentedList, removeAutoImport, removeAutoInclude, removeCustomAttribute, setAPIBuiltinEnabled, setArithmeticEngine, setAttemptExceptionReporter, setAutoFlush, setAutoImports, setAutoIncludes, setClassicCompatible, setClassicCompatibleAsInt, setCustomAttribute, setCustomDateFormats, setCustomNumberFormats, setLazyAutoImports, setLazyImports, setLogTemplateExceptions, setNewBuiltinClassResolver, setObjectWrapper, setSetting, setSettings, setSettings, setShowErrorTips, setStrictBeanModels, settingValueAssignmentException, setTruncateBuiltinAlgorithm, setWrapUncheckedExceptions, unknownSettingException
-
Constructor Details
-
Environment
Creates an environment with the given main (top-level) template that it intends to process() later; typically, it's better to useTemplate.createProcessingEnvironment(Object, Writer)
instead of this.- Parameters:
template
- Notnull
rootDataModel
- Notnull
out
- Notnull
-
-
Method Details
-
getCurrentEnvironment
Retrieves the environment object associated with the current thread, ornull
if there's no template processing going on in this 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. -
getTemplate
Deprecated.UsegetMainTemplate()
instead (orgetCurrentNamespace()
and thenEnvironment.Namespace.getTemplate()
); the value returned by this method is often not what you expect when it comes to macro/function invocations.Despite its name it just returnsConfigurable.getParent()
. IfConfiguration.getIncompatibleImprovements()
is at least 2.3.22, then that will be the same asgetMainTemplate()
. Otherwise the returned value follows theEnvironment
parent switchings that occur at#include
/#import
and#nested
directive calls, that is, it's not very meaningful outside FreeMarker internals. -
getMainTemplate
Returns the topmostTemplate
, with other words, the one for which thisEnvironment
was created. That template will never change, like#include
or macro calls don't change it. This method never returnsnull
.- Since:
- 2.3.22
- See Also:
getCurrentNamespace()
-
getCurrentTemplate
Returns theTemplate
that we are "lexically" inside at the moment. This template will change when entering an#include
or calling a macro or function in another template, or returning to yet another template with#nested
. When you are calling a directive that's implemented in Java or a Java method from a template, the current template will be the last current template, notnull
. This method never returnsnull
.- Since:
- 2.3.23
- See Also:
getMainTemplate()
,getCurrentNamespace()
-
getCurrentDirectiveCallPlace
Gets the currently executing custom directive's call place information, ornull
if there's no executing custom directive. This currently only works for calls made from templates with the<@...>
syntax. This should only be called from theTemplateDirectiveModel
that was invoked with<@...>
, otherwise its return value is not defined by this API (it's usuallynull
).- Since:
- 2.3.22
-
process
Processes the template to which this environment belongs to.- Throws:
TemplateException
IOException
-
visit
@Deprecated public void visit(freemarker.core.TemplateElement element, TemplateDirectiveModel directiveModel, Map args, List bodyParameterNames) throws TemplateException, IOExceptionDeprecated.Should be internal API- Throws:
TemplateException
IOException
-
isInAttemptBlock
public boolean isInAttemptBlock()Tells if we are inside an#attempt
block (but before#recover
). This can be useful forTemplateExceptionHandler
-s, as then they may don't want to print the error to the output, as#attempt
will roll it back anyway.- Since:
- 2.3.20
-
setTemplateExceptionHandler
Description copied from class:Configurable
Sets the exception handler used to handle exceptions occurring inside templates. The default isTemplateExceptionHandler.DEBUG_HANDLER
. The recommended values are:- In production systems:
TemplateExceptionHandler.RETHROW_HANDLER
- During development of HTML templates:
TemplateExceptionHandler.HTML_DEBUG_HANDLER
- During development of non-HTML templates:
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 fromTemplate.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. If you suppress an exception, and theConfigurable.getLogTemplateExceptions()
returnsfalse
, then it's the responsibility of theTemplateExceptionHandler
to log the exception (if you want it to be logged). - In production systems:
-
setLocale
Description copied from class:Configurable
Sets the locale used for number and date formatting (among others), also the locale used for searching localized template variations when no locale was explicitly requested. On theConfiguration
level it defaults to the default locale of system (of the JVM), for server-side application usually you should set it explicitly in theConfiguration
to use the preferred locale of your application instead.- Overrides:
setLocale
in classConfigurable
- See Also:
Configuration.getTemplate(String, Locale)
-
setTimeZone
Description copied from class:Configurable
Sets the time zone to use when formatting date/time values. Defaults to the system time zone (TimeZone.getDefault()
), regardless of the "locale" FreeMarker setting, so in a server application you probably want to set it explicitly in theEnvironment
to match the preferred time zone of target audience (like the Web page visitor).If you or the templates set the time zone, you should probably also set
Configurable.setSQLDateAndTimeTimeZone(TimeZone)
!- Overrides:
setTimeZone
in classConfigurable
- See Also:
Configurable.setSQLDateAndTimeTimeZone(TimeZone)
-
setSQLDateAndTimeTimeZone
Description copied from class:Configurable
Sets the time zone used when dealing withjava.sql.Date
andjava.sql.Time
values. It defaults tonull
for backward compatibility, but in most applications this should be set to the JVM default time zone (server default time zone), because that's what most JDBC drivers will use when constructing thejava.sql.Date
andjava.sql.Time
values. If this setting isnull
, FreeMarker will use the value of (Configurable.getTimeZone()
) forjava.sql.Date
andjava.sql.Time
values, which often gives bad results.This setting doesn't influence the formatting of other kind of values (like of
java.sql.Timestamp
or plainjava.util.Date
values).To decide what value you need, a few things has to be understood:
- Date-only and time-only values in SQL-oriented databases usually store calendar and clock field values directly (year, month, day, or hour, minute, seconds (with decimals)), as opposed to a set of points on the physical time line. Thus, unlike SQL timestamps, these values usually aren't meant to be shown differently depending on the time zone of the audience.
- When a JDBC query has to return a date-only or time-only value, it has to convert it to a point on the
physical time line, because that's what
Date
and its subclasses store (milliseconds since the epoch). Obviously, this is impossible to do. So JDBC just chooses a physical time which, when rendered with the JVM default time zone, will give the same field values as those stored in the database. (Actually, you can give JDBC a calendar, and so it can use other time zones too, but most application won't care using those overloads.) For example, assume that the system time zone is GMT+02:00. Then, 2014-07-12 in the database will be translated to physical time 2014-07-11 22:00:00 UTC, because that rendered in GMT+02:00 gives 2014-07-12 00:00:00. Similarly, 11:57:00 in the database will be translated to physical time 1970-01-01 09:57:00 UTC. Thus, the physical time stored in the returned value depends on the default system time zone of the JDBC client, not just on the content of the database. (This used to be the default behavior of ORM-s, like Hibernate, too.) - The value of the
time_zone
FreeMarker configuration setting sets the time zone used for the template output. For example, when a web page visitor has a preferred time zone, the web application framework may callssetTimeZone(TimeZone)
with that time zone. Thus, the visitor will seejava.sql.Timestamp
and plainjava.util.Date
values as they look in his own time zone. While this is desirable for those types, as they meant to represent physical points on the time line, this is not necessarily desirable for date-only and time-only values. Whensql_date_and_time_time_zone
isnull
,time_zone
is used for rendering all kind of date/time/dateTime values, includingjava.sql.Date
andjava.sql.Time
, and then if, for example,time_zone
is GMT+00:00, the values from the earlier examples will be shown as 2014-07-11 (one day off) and 09:57:00 (2 hours off). While those are the time zone correct renderings, those values are probably meant to be shown "as is". - You may wonder why this setting isn't simply "SQL time zone", that is, why's this time zone not applied to
java.sql.Timestamp
values as well. Timestamps in databases refer to a point on the physical time line, and thus doesn't have the inherent problem of date-only and time-only values. FreeMarker assumes that the JDBC driver converts time stamps coming from the database so that they store the distance from the epoch (1970-01-01 00:00:00 UTC), as requested by theDate
API. Then time stamps can be safely rendered in different time zones, and thus need no special treatment.
- Overrides:
setSQLDateAndTimeTimeZone
in classConfigurable
- Parameters:
timeZone
- Maybenull
, in which casejava.sql.Date
andjava.sql.Time
values will be formatted in the time zone returned byConfigurable.getTimeZone()
. (Note that sincenull
is an allowed value for this setting, it will not causeConfigurable.getSQLDateAndTimeTimeZone()
to fall back to the parent configuration.)- See Also:
Configurable.setTimeZone(TimeZone)
-
setURLEscapingCharset
Description copied from class:Configurable
Sets the URL escaping (URL encoding, percentage encoding) charset. Ifnull
, the output encoding (Configurable.setOutputEncoding(String)
) will be used for URL escaping. Defaults tonull
.- Overrides:
setURLEscapingCharset
in classConfigurable
-
setOutputEncoding
Description copied from class:Configurable
Informs FreeMarker about the charset used for the output. As FreeMarker outputs character stream (not byte stream), it's not aware of the output charset unless the software that encloses it tells it with this setting. Some templates may use FreeMarker features that require this information. Setting this tonull
means that the output encoding is not known.Defaults to
null
(unknown).- Overrides:
setOutputEncoding
in classConfigurable
-
applyEqualsOperator
public boolean applyEqualsOperator(TemplateModel leftValue, TemplateModel rightValue) throws TemplateExceptionCompares twoTemplateModel
-s according the rules of the FTL "==" operator.- Throws:
TemplateException
- Since:
- 2.3.20
-
applyEqualsOperatorLenient
public boolean applyEqualsOperatorLenient(TemplateModel leftValue, TemplateModel rightValue) throws TemplateExceptionCompares twoTemplateModel
-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.- Throws:
TemplateException
- Since:
- 2.3.20
-
applyLessThanOperator
public boolean applyLessThanOperator(TemplateModel leftValue, TemplateModel rightValue) throws TemplateExceptionCompares twoTemplateModel
-s according the rules of the FTL "<" operator.- Throws:
TemplateException
- Since:
- 2.3.20
-
applyLessThanOrEqualsOperator
public boolean applyLessThanOrEqualsOperator(TemplateModel leftValue, TemplateModel rightValue) throws TemplateExceptionCompares twoTemplateModel
-s according the rules of the FTL "<" operator.- Throws:
TemplateException
- Since:
- 2.3.20
-
applyGreaterThanOperator
public boolean applyGreaterThanOperator(TemplateModel leftValue, TemplateModel rightValue) throws TemplateExceptionCompares twoTemplateModel
-s according the rules of the FTL ">" operator.- Throws:
TemplateException
- Since:
- 2.3.20
-
applyWithGreaterThanOrEqualsOperator
public boolean applyWithGreaterThanOrEqualsOperator(TemplateModel leftValue, TemplateModel rightValue) throws TemplateExceptionCompares twoTemplateModel
-s according the rules of the FTL ">=" operator.- Throws:
TemplateException
- Since:
- 2.3.20
-
setOut
-
getOut
-
setNumberFormat
Description copied from class:Configurable
Sets the number format used to convert numbers to strings. Currently, this is one of these:"number"
: The number format returned byNumberFormat.getNumberInstance(Locale)
. This is the default."c"
(recognized since 2.3.32): The number format used by FTL'sc
built-in (like insomeNumber?c
). So with this${someNumber}
will output the same as${someNumber?c}
. This should only be used if the template solely generates source code, configuration file, or other content that's not read by normal users. If the template contains parts that's read by normal users (like typical a web page), you are not supposed to use this."computer"
: The old (deprecated) name for"c"
. Recognized by all FreeMarker versions."currency"
: The number format returned byNumberFormat.getCurrencyInstance(Locale)
"percent"
: The number format returned byNumberFormat.getPercentInstance(Locale)
DecimalFormat
pattern (like"0.##"
). This syntax is extended by FreeMarker so that you can specify options like the rounding mode and the symbols used after a 2nd semicolon. For example,",000;; roundingMode=halfUp groupingSeparator=_"
will format numbers like",000"
would, but with half-up rounding mode, and_
as the group separator. See more about "extended Java decimal format" in the FreeMarker Manual.- If the string starts with
@
character followed by a letter then it's interpreted as a custom number format, but only if eitherConfiguration.getIncompatibleImprovements()
is at least 2.3.24, or there's any custom formats defined (even if custom date/time/dateTime format). The format of a such string is"@name"
or"@name parameters"
, wherename
is the key in theMap
set byConfigurable.setCustomNumberFormats(Map)
, andparameters
is parsed by the customTemplateNumberFormat
.
Defaults to
"number"
.- Overrides:
setNumberFormat
in classConfigurable
-
getTemplateNumberFormat
Returns the current number format (Configurable.getNumberFormat()
) asTemplateNumberFormat
.Performance notes: The result is stored for reuse, so calling this method frequently is usually not a problem. However, at least as of this writing (2.3.24), changing the current locale
setLocale(Locale)
or changing the current number format (setNumberFormat(String)
) will drop the stored value, so it will have to be recalculated.- Throws:
TemplateValueFormatException
- Since:
- 2.3.24
-
getTemplateNumberFormat
public TemplateNumberFormat getTemplateNumberFormat(String formatString) throws TemplateValueFormatExceptionReturns the number format asTemplateNumberFormat
for the given format string and the current locale. (The current locale is the locale returned byConfigurable.getLocale()
.) Note that the result will be cached in theEnvironment
instance (though at least in 2.3.24 the cache will be flushed if the current locale of theEnvironment
is changed).- Parameters:
formatString
- A string that you could also use as the value of thenumberFormat
configuration setting. Can't benull
.- Throws:
TemplateValueFormatException
- Since:
- 2.3.24
-
getTemplateNumberFormat
public TemplateNumberFormat getTemplateNumberFormat(String formatString, Locale locale) throws TemplateValueFormatExceptionReturns the number format asTemplateNumberFormat
, for the given format string and locale. To get a number format for the current locale, usegetTemplateNumberFormat(String)
instead.Note on performance (which was true at least for 2.3.24): Unless the locale happens to be equal to the current locale, the
Environment
-level format cache can't be used, so the format string has to be parsed and the matching factory has to be get an invoked, which is much more expensive than getting the format from the cache. Thus the returned format should be stored by the caller for later reuse (but only within the current thread and in relation to the currentEnvironment
), if it will be needed frequently.- Parameters:
formatString
- A string that you could also use as the value of thenumberFormat
configuration setting.locale
- The locale of the number format; notnull
.- Throws:
TemplateValueFormatException
- Since:
- 2.3.24
-
getCNumberFormat
Deprecated.UsegetCTemplateNumberFormat()
instead. This method can't return the format used when Incompatible Improvements is 2.3.32, or greater, and instead it will fall back to return the format that was used for 2.3.31. Also, as its described earlier, this method was inconsistent with?c
between Incompatible Improvements 2.3.21 and 2.3.30, whilegetCTemplateNumberFormat()
behaves as?c
for all Incompatible Improvements value.Returns theNumberFormat
used for thec
built-in, except, if Incompatible Improvements is less than 2.3.31, this will wrongly give the format that thec
built-in used before Incompatible Improvements 2.3.21. See more atConfiguration(Version)
. -
getCTemplateNumberFormat
Returns theTemplateNumberFormat
that?c
/?cn
uses. Calling this method for many times is fine, as it internally caches the result object. Remember thatTemplateNumberFormat
-s aren't thread-safe objects, so the resulting object should only be used in the same thread where thisEnvironment
runs.- Since:
- 2.3.32
-
setCFormat
Description copied from class:Configurable
Sets the format (usually a computer language) used for?c
,?cn
, and for the"c"
("computer"
before 2.3.32)number_format
, and the"c"
boolean_format
.The default value depends on
incompatible_improvements
. If that's 2.3.32 or higher, then it's"JavaScript or JSON"
, otherwise it's"legacy"
.- Overrides:
setCFormat
in classConfigurable
-
setTimeFormat
Description copied from class:Configurable
Sets the format used to convertDate
-s that are time (no date part) values to string-s, also the format thatsomeString?time
will use to parse strings.For the possible values see
Configurable.setDateTimeFormat(String)
.Defaults to
""
, which is equivalent to"medium"
.- Overrides:
setTimeFormat
in classConfigurable
-
setDateFormat
Description copied from class:Configurable
Sets the format used to convertDate
-s that are date-only (no time part) values to string-s, also the format thatsomeString?date
will use to parse strings.For the possible values see
Configurable.setDateTimeFormat(String)
.Defaults to
""
which is equivalent to"medium"
.- Overrides:
setDateFormat
in classConfigurable
-
setDateTimeFormat
Description copied from class:Configurable
Sets the format used to convertDate
-s that are date-time (timestamp) values to string-s, also the format thatsomeString?datetime
will use to parse strings.The possible setting values are (the quotation marks aren't part of the value itself):
Patterns accepted by Java's
SimpleDateFormat
, for example"dd.MM.yyyy HH:mm:ss"
(whereHH
means 24 hours format) or"MM/dd/yyyy hh:mm:ss a"
(wherea
prints AM or PM, if the current language is English)."xs"
for XML Schema format, or"iso"
for ISO 8601:2004 format. These formats allow various additional options, separated with space, like in"iso m nz"
(or with_
, like in"iso_m_nz"
; this is useful in a case likelastModified?string.iso_m_nz
). The options and their meanings are:Accuracy options:
ms
= Milliseconds, always shown with all 3 digits, even if it's all 0-s. Example:13:45:05.800
s
= Seconds (fraction seconds are dropped even if non-0), like13:45:05
m
= Minutes, like13:45
. This isn't allowed for "xs".
h
= Hours, like13
. This isn't allowed for "xs".
Neither = Up to millisecond accuracy, but trailing millisecond 0-s are removed, also the whole milliseconds part if it would be 0 otherwise. Example:13:45:05.8
Time zone offset visibility options:
fz
= "Force Zone", always show time zone offset (even for forjava.sql.Date
andjava.sql.Time
values). But, because ISO 8601 doesn't allow for dates (means date without time of the day) to show the zone offset, this option will have no effect in the case of"iso"
with dates.
nz
= "No Zone", never show time zone offset
Neither = always show time zone offset, except forjava.sql.Date
andjava.sql.Time
, and for"iso"
date values.Time zone options:
u
= Use UTC instead of what thetime_zone
setting suggests. However,java.sql.Date
andjava.sql.Time
aren't affected by this (seeConfigurable.setSQLDateAndTimeTimeZone(TimeZone)
to understand why)
fu
= "Force UTC", that is, use UTC instead of what thetime_zone
or thesql_date_and_time_time_zone
setting suggests. This also effectsjava.sql.Date
andjava.sql.Time
values
Neither = Use the time zone suggested by thetime_zone
or thesql_date_and_time_time_zone
configuration setting (Configurable.setTimeZone(TimeZone)
andConfigurable.setSQLDateAndTimeTimeZone(TimeZone)
).
The options can be specified in any order.
Options from the same category are mutually exclusive, like using
m
ands
together is an error.The accuracy and time zone offset visibility options don't influence parsing, only formatting. For example, even if you use "iso m nz", "2012-01-01T15:30:05.125+01" will be parsed successfully and with milliseconds accuracy. The time zone options (like "u") influence what time zone is chosen only when parsing a string that doesn't contain time zone offset.
Parsing with
"iso"
understands both extend format and basic format, like20141225T235018
. It doesn't, however, support the parsing of all kind of ISO 8601 strings: if there's a date part, it must use year, month and day of the month values (not week of the year), and the day can't be omitted.The output of
"iso"
is deliberately so that it's also a good representation of the value with XML Schema format, except for 0 and negative years, where it's impossible. Also note that the time zone offset is omitted for date values in the"iso"
format, while it's preserved for the"xs"
format."short"
,"medium"
,"long"
, or"full"
, which that has locale-dependent meaning defined by the Java platform (see in the documentation ofDateFormat
). For date-time values, you can specify the length of the date and time part independently, be separating them with_
, like"short_medium"
. ("medium"
means"medium_medium"
for date-time values.)Anything that starts with
"@"
followed by a letter is interpreted as a custom date/time/dateTime format, but only if eitherConfiguration.getIncompatibleImprovements()
is at least 2.3.24, or there's any custom formats defined (even if custom number format). The format of such string is"@name"
or"@name parameters"
, wherename
is the key in theMap
set byConfigurable.setCustomDateFormats(Map)
, andparameters
is parsed by the custom number format.
Defaults to
""
, which is equivalent to"medium_medium"
.- Overrides:
setDateTimeFormat
in classConfigurable
-
setBooleanFormat
Description copied from class:Configurable
The string value for the booleantrue
andfalse
values, usually intended for human consumption (not for a computer language), separated with comma. For example,"yes,no"
. Note that white-space is significant, so"yes, no"
is WRONG (unless you want that leading space before "no"). Because the proper way of formatting booleans depends on the context too much, it's probably the best to leave this setting on its default, which will enforce explicit formatting, like${aBoolean?string('on', 'off')}
.For backward compatibility the default is
"true,false"
, but using that value is denied for automatic boolean-to-string conversion, like${myBoolean}
will fail with it. If you generate the piece of output for "computer audience" as opposed to "human audience", then you should write${myBoolean?c}
, which will printtrue
orfalse
. If you really want to always format for computer audience, then it's might be reasonable to set this setting toc
.Note that automatic boolean-to-string conversion only exists since FreeMarker 2.3.20. Earlier this setting only influenced the result of
myBool?string
.- Overrides:
setBooleanFormat
in classConfigurable
-
getConfiguration
-
getTemplateDateFormat
public TemplateDateFormat getTemplateDateFormat(int dateType, Class<? extends Date> dateClass) throws TemplateValueFormatExceptionGets aTemplateDateFormat
using the date/time/datetime format settings and the current locale and time zone. (The current locale is the locale returned byConfigurable.getLocale()
. The current time zone isConfigurable.getTimeZone()
orConfigurable.getSQLDateAndTimeTimeZone()
).- Parameters:
dateType
- The FTL date type; see the similar parameter ofTemplateDateFormatFactory.get(String, int, Locale, TimeZone, boolean, Environment)
dateClass
- The exactDate
class, likeDate
orTime
; this can influences time zone selection. See also:setSQLDateAndTimeTimeZone(TimeZone)
- Throws:
TemplateValueFormatException
-
getTemplateDateFormat
public TemplateDateFormat getTemplateDateFormat(String formatString, int dateType, Class<? extends Date> dateClass) throws TemplateValueFormatExceptionGets aTemplateDateFormat
for the specified format string and the current locale and time zone. (The current locale is the locale returned byConfigurable.getLocale()
. The current time zone isConfigurable.getTimeZone()
orConfigurable.getSQLDateAndTimeTimeZone()
).Note on performance: The result will be cached in the
Environment
instance. However, at least in 2.3.24 the cached entries that depend on the current locale or the current time zone or the current date/time/datetime format of theEnvironment
will be lost when those settings are changed.- Parameters:
formatString
- Like"iso m"
or"dd.MM.yyyy HH:mm"
or"@somethingCustom"
or"@somethingCustom params"
- Throws:
TemplateValueFormatException
- Since:
- 2.3.24
-
getTemplateDateFormat
public TemplateDateFormat getTemplateDateFormat(String formatString, int dateType, Class<? extends Date> dateClass, Locale locale) throws TemplateValueFormatExceptionLikegetTemplateDateFormat(String, int, Class)
, but allows you to use a different locale than the current one. If you want to use the current locale, usegetTemplateDateFormat(String, int, Class)
instead.Performance notes regarding the locale and time zone parameters of
getTemplateDateFormat(String, int, Locale, TimeZone, boolean)
apply.- Parameters:
locale
- Can't benull
; See the similar parameter ofTemplateDateFormatFactory.get(String, int, Locale, TimeZone, boolean, Environment)
- Throws:
TemplateValueFormatException
- Since:
- 2.4
- See Also:
getTemplateDateFormat(String, int, Class)
-
getTemplateDateFormat
public TemplateDateFormat getTemplateDateFormat(String formatString, int dateType, Class<? extends Date> dateClass, Locale locale, TimeZone timeZone, TimeZone sqlDateAndTimeTimeZone) throws TemplateValueFormatExceptionLikegetTemplateDateFormat(String, int, Class)
, but allows you to use a different locale and time zone than the current one. If you want to use the current locale and time zone, usegetTemplateDateFormat(String, int, Class)
instead.Performance notes regarding the locale and time zone parameters of
getTemplateDateFormat(String, int, Locale, TimeZone, boolean)
apply.- Parameters:
timeZone
- TheTimeZone
used ifdateClass
is not an SQL date-only or time-only type. Can't benull
.sqlDateAndTimeTimeZone
- TheTimeZone
used ifdateClass
is an SQL date-only or time-only type. Can't benull
.- Throws:
TemplateValueFormatException
- Since:
- 2.4
- See Also:
getTemplateDateFormat(String, int, Class)
-
getTemplateDateFormat
public TemplateDateFormat getTemplateDateFormat(String formatString, int dateType, Locale locale, TimeZone timeZone, boolean zonelessInput) throws TemplateValueFormatExceptionGets aTemplateDateFormat
for the specified parameters. This is mostly meant to be used byTemplateDateFormatFactory
implementations to delegate to a format based on a specific format string. It works well for that, as its parameters are the same low level values as the parameters ofTemplateDateFormatFactory.get(String, int, Locale, TimeZone, boolean, Environment)
. For other tasks consider the other overloads of this method.Note on performance (which was true at least for 2.3.24): Unless the locale happens to be equal to the current locale and the time zone with one of the current time zones (
Configurable.getTimeZone()
orConfigurable.getSQLDateAndTimeTimeZone()
), theEnvironment
-level format cache can't be used, so the format string has to be parsed and the matching factory has to be get an invoked, which is much more expensive than getting the format from the cache. Thus the returned format should be stored by the caller for later reuse (but only within the current thread and in relation to the currentEnvironment
), if it will be needed frequently.- Parameters:
formatString
- Like"iso m"
or"dd.MM.yyyy HH:mm"
or"@somethingCustom"
or"@somethingCustom params"
dateType
- The FTL date type; see the similar parameter ofTemplateDateFormatFactory.get(String, int, Locale, TimeZone, boolean, Environment)
timeZone
- Notnull
; See the similar parameter ofTemplateDateFormatFactory.get(String, int, Locale, TimeZone, boolean, Environment)
locale
- Notnull
; See the similar parameter ofTemplateDateFormatFactory.get(String, int, Locale, TimeZone, boolean, Environment)
zonelessInput
- See the similar parameter ofTemplateDateFormatFactory.get(String, int, Locale, TimeZone, boolean, Environment)
- Throws:
TemplateValueFormatException
- Since:
- 2.3.24
-
getLocalVariable
Returns the loop or macro local variable corresponding to this variable name. Returnsnull
if no such variable exists with the given name, or the variable was set tonull
. Doesn't read namespace or global variables.- Throws:
TemplateModelException
-
getVariable
Returns the variable that is visible in this context, ornull
if the variable is not found. 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(...)
- Throws:
TemplateModelException
-
getGlobalVariable
Returns the globally visible variable of the given name, ornull
. This corresponds 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, and then at the Configuration.setSharedVariables(Map) shared variables} in theConfiguration
.- Throws:
TemplateModelException
-
setGlobalVariable
Sets a variable in the global namespace, like<#global name=value>
. This can be considered a convenient shorthand forgetGlobalNamespace().put(name, model)
.Note that this is not an exact pair of
getGlobalVariable(String)
, as that falls back to higher scopes if the variable is not in the global namespace.- Parameters:
name
- The name of the variable.value
- The new value of the variable.null
in effect removes the local variable (reading it will fall back to higher scope).
-
setVariable
Sets a variable in the current namespace, like<#assign name=value>
. This can be considered a convenient shorthand for:getCurrentNamespace().put(name, model)
.- Parameters:
name
- The name of the variable.value
- The new value of the variable.null
in effect removes the local variable (reading it will fall back to higher scope).
-
setLocalVariable
Sets a local variable that's on the top-level inside a macro or function invocation, like<#local name=value>
. Note that just like<#local name=value>
, this will not set loop variables; it will totally ignore them, and might sets a local variable that a loop variable currently "shadows". As such, it's not exactly the pair ofgetLocalVariable(String)
, which also reads loop variables.- Parameters:
name
- The name of the variable.value
- The new value of the variable.null
in effect removes the local variable (reading it will fall back to higher scope).- Throws:
IllegalStateException
- if the environment is not executing a macro body.
-
getKnownVariableNames
Returns a set of variable names that are known at the time of call. This includes names of all shared variables in theConfiguration
, 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 theTemplateHashModelEx
interface, then all names it retrieves through a call toTemplateHashModelEx.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.- Throws:
TemplateModelException
-
outputInstructionStack
Prints the current FTL stack trace. Useful for debugging.TemplateException
s incorporate this information in their stack traces. -
getNamespace
Returns the name-space for the name if exists, or null.- Parameters:
name
- the template path that you have used with theimport
directive orimportLib(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.
-
getMainNamespace
Returns the main namespace. This corresponds to the FTL.main
hash. -
getCurrentNamespace
Returns the current namespace. This corresponds to the FTL.namespace
hash. Initially, the current name space is the main namespace, but when inside an#import
-ed template, it will change to the namespace of that import. Note that#include
doesn't affect the namespace, so if you are in an#import
-ed template and then from there do an#include
, the current namespace will remain the namespace of the#import
. -
getGlobalNamespace
Returns the name-space that contains the globally visible non-data-model variables (usually created with<#global ...>
). -
getDataModel
Returns a view of the data-model (also known as the template context in some other template engines) that falls back to shared variables. -
getGlobalVariables
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, usesetGlobalVariable
. -
setTemplateProcessingTracer
Sets theTemplateProcessingTracer
to use for thisEnvironment
; can benull
to not have one. The default is alsonull
.- Since:
- 2.3.33
-
getTemplateProcessingTracer
Getter pair ofsetTemplateProcessingTracer(TemplateProcessingTracer)
. Can benull
.- Since:
- 2.3.33
-
getCurrentVisitorNode
-
setCurrentVisitorNode
sets TemplateNodeModel as the current visitor node..current_node
-
include
public void include(String name, String encoding, boolean parse) throws IOException, TemplateExceptionEmulatesinclude
directive, except thatname
must be template 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. -
getTemplateForInclusion
public Template getTemplateForInclusion(String name, String encoding, boolean parse) throws IOExceptionSame asgetTemplateForInclusion(String, String, boolean, boolean)
withfalse
ignoreMissing
argument.- Throws:
IOException
-
getTemplateForInclusion
public Template getTemplateForInclusion(String name, String encoding, boolean parseAsFTL, boolean ignoreMissing) throws IOExceptionGets a template for inclusion; used for implementinginclude(Template includedTemplate)
. The advantage over simply usingconfig.getTemplate(...)
is that it chooses the default encoding exactly as theinclude
directive does, although that encoding selection mechanism is a historical baggage and considered to be harmful.- 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 useTemplateCache.getFullTemplatePath(freemarker.core.Environment, java.lang.String, java.lang.String)
to convert paths to template root relative paths.) For more details see the identical parameter ofConfiguration.getTemplate(String, Locale, String, boolean, boolean)
encoding
- the charset of the obtained template. Ifnull
, the encoding of the top template that is currently being processed in thisEnvironment
is used, which can lead to odd situations, so usingnull
is not recommended. In most applications, the value ofConfiguration.getEncoding(Locale)
(orConfiguration.getDefaultEncoding()
) should be used here.parseAsFTL
- See identical parameter ofConfiguration.getTemplate(String, Locale, String, boolean, boolean)
ignoreMissing
- See identical parameter ofConfiguration.getTemplate(String, Locale, String, boolean, boolean)
- Returns:
- Same as
Configuration.getTemplate(String, Locale, String, boolean, boolean)
- Throws:
IOException
- Same as exceptions thrown byConfiguration.getTemplate(String, Locale, String, boolean, boolean)
- Since:
- 2.3.21
-
include
Processes a Template in the context of thisEnvironment
, including its output in theEnvironment
's Writer.- Parameters:
includedTemplate
- the template to process. Note that it does not need to be a template returned bygetTemplateForInclusion(String name, String encoding, boolean parse)
.- Throws:
TemplateException
IOException
-
importLib
public Environment.Namespace importLib(String templateName, String targetNsVarName) throws IOException, TemplateExceptionEmulatesimport
directive, except thattemplateName
must be template root relative.It's the same as
importLib(getTemplateForImporting(templateName), 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.If it will be a lazy or an eager import is decided by the value of
Configurable.getLazyImports()
. You can also directly control that aspect by usingimportLib(String, String, boolean)
instead.- Returns:
- Not
null
. This is possibly a lazily self-initializing namespace, which means that it will only try to get and process the imported template when you access its content. - Throws:
IOException
TemplateException
- See Also:
getTemplateForImporting(String templateName)
,importLib(Template includedTemplate, String namespaceVarName)
,importLib(String, String, boolean)
-
importLib
public Environment.Namespace importLib(Template loadedTemplate, String targetNsVarName) throws IOException, TemplateExceptionDoes what the#import
directive does, but with an already loaded template.- Parameters:
loadedTemplate
- The template to import. Note that it does not need to be a template returned bygetTemplateForImporting(String name)
. Notnull
.targetNsVarName
- The name of the FTL variable that will store the namespace. Ifnull
, the namespace won't be stored in a variable (but it's still returned).- Returns:
- The namespace of the imported template, already initialized.
- Throws:
IOException
TemplateException
- See Also:
getTemplateForImporting(String name)
,importLib(Template includedTemplate, String namespaceVarName)
-
importLib
public Environment.Namespace importLib(String templateName, String targetNsVarName, boolean lazy) throws IOException, TemplateExceptionLikeimportLib(String, String)
, but you can specify if you want a lazy import or not.- Returns:
- Not
null
. This is possibly a lazily self-initializing namespace, which mean that it will only try to get and process the imported template when you access its content. - Throws:
IOException
TemplateException
- Since:
- 2.3.25
-
getTemplateForImporting
Gets a template for importing; used withimportLib(Template importedTemplate, String namespace)
. The advantage over simply usingconfig.getTemplate(...)
is that it chooses the encoding as theimport
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 useTemplateCache.getFullTemplatePath(freemarker.core.Environment, java.lang.String, java.lang.String)
to convert paths to template root relative paths.)- Throws:
IOException
-
toFullTemplateName
public String toFullTemplateName(String baseName, String targetName) throws MalformedTemplateNameExceptionResolves a reference to a template (like the one used in#include
or#import
), assuming a base name. This gives a root based, even if non-normalized and possibly non-absolute (but then relative to the root) template name, that could be used forConfiguration.getTemplate(String)
. This is mostly used when a template refers to another template.If you need to guarantee that the result is also an absolute path, then apply
rootBasedToAbsoluteTemplateName(String)
on it.- Parameters:
baseName
- The name to which relativetargetName
-s are relative to. Maybenull
(happens when resolving names in nameless templates), which means that the base is the root "directory", and so thetargetName
is returned without change. AssumingTemplateNameFormat.DEFAULT_2_3_0
orTemplateNameFormat.DEFAULT_2_4_0
, the rules are as follows. If you want to specify a base directory here, it must end with"/"
. If it doesn't end with"/"
, it's parent directory will be used as the base path. Might starts with a scheme part (like"foo://"
, or withTemplateNameFormat.DEFAULT_2_4_0
even just with"foo:"
).targetName
- The name of the template, which is either a relative or absolute name. AssumingTemplateNameFormat.DEFAULT_2_3_0
orTemplateNameFormat.DEFAULT_2_4_0
, the rules are as follows. If it starts with"/"
or contains a scheme part separator ("://"
, also, withTemplateNameFormat.DEFAULT_2_4_0
a":"
with no"/"
anywhere before it) then it's an absolute name, otherwise it's a relative path. Relative paths are interpreted relatively to thebaseName
. Absolute names are simply returned as is, ignoring thebaseName
, except, when thebaseName
has scheme part while thetargetName
doesn't have, then the schema of thebaseName
is prepended to thetargetName
.- Throws:
MalformedTemplateNameException
- Since:
- 2.3.22
-
rootBasedToAbsoluteTemplateName
public String rootBasedToAbsoluteTemplateName(String rootBasedName) throws MalformedTemplateNameExceptionConverts a root based name (a name that's either relative to the root, or is absolute), which are typically used by the API (such as forConfiguration.getTemplate(String)
), to an absolute name, which can be safely passed to<#include path>
and such, as it won't be misinterpreted to be relative to the directory of the template. For example,"foo/bar.ftl"
is converted to"/foo/bar.ftl"
, while"/foo/bar"
or"foo://bar/baz"
remains as is, as they are already absolute names (seeTemplateNameFormat
for more about the format of names).You only need this if the template name will be passed to
<#include name>
,<#import name>
,.get_optional_template(name)
or a similar construct in a template, otherwise using non-absolute root based names is fine.- Throws:
MalformedTemplateNameException
- Since:
- 2.3.28
-
getNamespaceForPrefix
- Returns:
- the namespace URI registered for this prefix, or null. This is based on the mappings registered in the current namespace.
-
getPrefixForNamespace
-
getDefaultNS
- Returns:
- the default node namespace for the current FTL namespace
-
__getitem__
A hook that Jython uses.- Throws:
TemplateModelException
-
__setitem__
A hook that Jython uses.- Throws:
TemplateException
-
getCustomState
Returns the value of a custom state variable, ornull
if it's missing; seesetCustomState(Object, Object)
for more.- Since:
- 2.3.24
-
setCustomState
Sets the value of a custom state variable. Custom state variables meant to be used byTemplateNumberFormatFactory
-es,TemplateDateFormatFactory
-es, and similar user-implementable, pluggable objects, which want to maintain anEnvironment
-scoped state (such as a cache).- Parameters:
identityKey
- The key that identifies the variable, by its object identity (not byObject.equals(Object)
). This should be something like aprivate static final Object CUSTOM_STATE_KEY = new Object();
in the class that needs this state variable.value
- The value of the variable. Can be anything, evennull
.- Returns:
- The previous value of the variable, or
null
if the variable didn't exist. - Since:
- 2.3.24
-
getCTemplateNumberFormat()
instead.