Special Variable Reference

Special variables are variables defined by the FreeMarker engine itself. To access them, you use the .variable_name syntax. For example, you can't write simply version; you have to write .version.

Note:

As of FreeMarker 2.3.23, you can use camel case instead of snake case for special variable names, like dataModel instead of data_model. But know that then within the same template, FreeMarker will enforce the usage of camel case for all identifiers that are part of the template language (user defined names are not affected).

The supported special variables are:

Using get_optional_template

This special variable is used when you need to include or import a template that's possibly missing, and you need to handle that case on some special way. It a method (so you meant to call it) that has the following parameters:

  1. The name of the template (can be relative or absolute), like "/commonds/footer.ftl"; similar to the first parameter of the include directive. Required, string.

  2. An optional hash of options, like { 'parse': false, 'encoding': 'UTF-16BE' }. The valid keys are encoding and parse. The meaning of these are the same as of the similarly named include directive parameters.

This method returns a hash that contains the following entries:

When this method is called (like .get_optional_template('some.ftl')), it immediately loads the template if it exists, but doesn't yet process it, so it doesn't have any visible effect yet. The template will be processed only when include or import members of the returned structure is called. (Of course, when we say that it loads the template, it actually looks it up in the template cache, just like the include directive does.)

While it's not an error if the template is missing, it's an error if it does exist but still can't be loaded due to syntactical errors in it, or due to some I/O error.

Note:

If the template fails with "I/O error when trying to load optional template" when the template is missing, that's often because your application uses a custom freemarker.cache.TemplateLoader implementation, which incorrectly (against the API documentation) throws an IOException in the findTemplateSource method instead of returning null if a template is not found. If it's so, the Java programmers need to fix that. Another possibility is of course that it was indeed not possible to tell if the template exists or not due to some technical issues, in which case stopping with error is the correct behavior. See the cause IOException in the Java stack trace to figure out which case it is.

Example, in which depending on if some.ftl exists, we either print "Template was found:" and the include the template as <#include 'some.ftl'> would, otherwise it we print "Template was missing.":

<#assign optTemp = .get_optional_template('some.ftl')>
<#if optTemp.exists>
  Template was found:
  <@optTemp.include />
<#else>
  Template was missing.
</#if>

Example, in which we try to include some.ftl, but if that's missing then we try to include some-fallback.ftl, and if that's missing too then we call the ultimateFallback macro instead of including anything (note the !-s after the include-s; they belong to the exp!default operator):

<#macro ultimateFallback>
  Something
</#macro>

<@(
  .get_optional_template('some.ftl').include!
  .get_optional_template('some-fallback.ftl').include!
  ultimateFallback
) />

Example, which behaves like <#import 'tags.ftl' as tags>, except that if tags.ftl is missing, then it creates an empty tags hash:

<#assign tags = (.get_optional_template('tags.ftl').import())!{}>
上一章 首页 下一章