include

Page Contents

Synopsis

<#include path>
or
<#include path options>

Where:

Description

You can use it to insert another FreeMarker template file (specified by the path parameter) into your template. The output from the included template is inserted at the point where the include tag occurs. The included file shares the variables with the including template, similarly like if it was copy-pasted into it. The include directive is not replaced by the content of the included file, it just processes the included file each time when FreeMarker reaches the include directive in the course of template processing. So for example if the include is inside a listloop, you can specify different file names in each cycle.

Note

This directive is not be confused with the JSP (Servlet) include, as it doesn't involve the Servlet container at all, just processes another FreeMarker template, without "leaving" FreeMarker. Regarding how to do a "JSP include" read this...

The path parameter can be a relative path like "foo.ftl" and "../foo.ftl", or an absolute like "/foo.ftl". Relative paths are relative to the directory of the template that uses the import directive. Absolute paths are relative to a base (often referred as the ''root directory of the templates'') that the programmer defines when he configures FreeMarker.

Note

This is different than the way it worked prior FreeMarker 2.1, where the path was always absolute. To preserve the old behavior, enable the classic compatible mode in the Configuration object.

Always use / (slash) to separate path components, never \ (backslash). If you are loading templates from your local file system and it uses backslashes (like under. Windows), FreeMarker will convert them automatically.

Example:

Assume /common/copyright.ftl contains:

Copyright 2001-2002 ${me}<br>
All rights reserved.  

Then this:

<#assign me = "Juila Smith">
<h1>Some test</h1>
<p>Yeah.
<hr>
<#include "/common/copyright.ftl">  

will output this:

<h1>Some test</h1>
<p>Yeah.
<hr>
Copyright 2001-2002 Juila Smith
All rights reserved.  

The supported options are:

Example:

<#include "/common/navbar.html" parse=false encoding="Shift_JIS">  

Note, that it is possible to automatically do the commonly used inclusions for all templates, with the "auto includes" setting of Configuration.

Using acquisition

There's a special path component represented by an asterisk (*). It is interpreted as "this directory or any of its parents". Therefore, if the template located in /foo/bar/template.ftl has the following line:

<#include "*/footer.ftl">  

then the engine will look for the template in following locations, in this order:

This mechanism is called acquisition and allows the designers to place commonly included files in a parent directory, and redefine them on a per-subdirectory basis as needed. We say that the including template acquires the template to include from the first parent directory that has it. Note that you can specify not only a template name to the right of the asterisk, but a subpath as well. I.e. if the previous template instead read:

<#include "*/commons/footer.ftl">  

then the engine would look for the template in following locations, in this order:

Finally, the asterisk needn't be the first element of the path:

<#include "commons/*/footer.ftl">  

would cause the engine to look for the template in following locations, in this order:

However, there can be at most one asterisk in the path. Specifying more than one asterisk will result in a template not being found.

Localized lookup

Whenever a template is loaded, it is assigned a locale. A locale is a language and an optional country or dialect identifier. A template is typically loaded by some code that the programmer wrote and he chooses a locale for the template based on some aspect. For example, when the FreemarkerServlet loads templates, it always requests the template with locale matching the language preference of the browser that requested the web page.

When a template includes another template, it attempts to load a template with the same locale. Suppose your template was loaded with locale en_US, which means U.S. English. When you include another template:

<include "footer.ftl">  

the engine will in fact look for several templates, in this order:

Note that you can disable localized lookup feature with the setLocalizedLookup method of Configuration.

When you use both acquisition and localized template lookup, the template with more specific locale in a parent directory takes precedence over template with less specific locale in a child directory. Suppose you use the following include from /foo/bar/template.ftl:

<#include "*/footer.ftl">  

the engine will look for these templates, in this order:

FreeMarker Manual -- For FreeMarker 2.3.20
HTML generated: 2013-06-27 20:54:33 GMT
Edited with XMLMind XML Editor
Here!