Including Templates within a Template

If you're using a TemplateCache (described here) to store your templates, you can include a template inside another template, using this syntax:

<include name>
	

The name is an expression whose string value is the unique name of the template in the cache. For example, if you're using a FileTemplateCache:

<include "foo-templates/template-to-include.html">
	

The output from the included template is inserted at the point where the include statement occurs. The included template uses the including template's data model; any assignments made in the included template therefore remain in effect in the including template. Functions defined in either template can be called in the other template. This makes an included template a convenient place to put constants and functions used on several pages.

Since include statements are processed at run time, they should occur above any references to included variables or functions.

Other files can be included as well. If you need to include a file that shouldn't/doesn't need to be compiled by the FreeMarker parser, these can be included as follows:

<include "foo-files/file-to-include.inc"; parse="n">
	

There are two optional attributes that can be expressed after a semicolon. You can set the include to treat the file as a plain file, i.e. unparsed. You can also set the encoding of the file. If this is not set, the included file is assumed to be stored in the same encoding as the enclosing template. This, of course, is the usual situation and most people will have little reason to use these attributes.

<include "included.txt" ; parse="n" encoding="ISO-8859-5">
	

The code above says to include the file "included.txt", to treat it as a plain file, and that it is encoded in the "ISO-8859-5" character encoding. Note that the file to be included need not be a literal string. It could also be a variable or other expression that resolves to the filename.