How are file templates defined in NetBeans IDE? What is FreeMarker?

File templates are used by the wizards in the New File wizard, when you create new files. For example, when you create a new JSP in the New File wizard, the wizard takes the values you fill in (such as the JSP name) and inserts them into the JSP file template, resulting in a JSP file with the name you specified.

The IDE comes with a set of predefined file templates, which you can see in the Templates Manager, under the Tools menu. You can modify most of these file templates and you can add your own too.

About FreeMarker

In NetBeans IDE, you can define file templates in two different ways. The first is using plain text. The second way is to use FreeMarker. FreeMarker is a templating language, available outside NetBeans. However, since it is common for programmers to use FreeMarker to define their templates, NetBeans IDE bundles the FreeMarker templating engine so that you can use the FreeMarker template language to define your templates.

For example, this is how the Java Class template that comes with the IDE is defined:

<#assign licenseFirst = "/*">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#include "../Licenses/license-${project.license}.txt">

<#if package?? && package != "">
package ${package};

 * @author ${user}
public class ${name} {


Above, you can see tags like <#if>, which are FreeMarker directives. Here is a complete list:

Use FreeMarker directives to add logic to your templates. For details on the FreeMarker directives, see the FreeMarker Directives chapter and the Directive Reference in the FreeMarker Manual. For information on the "license" tags (i.e., the first 4 lines in the snippet above), see Project-Level License Settings in NetBeans IDE 6.0.

When to Use FreeMarker

You do not HAVE TO use FreeMarker. Doing so only makes sense if you want to add logic (i.e., if/else/loops/etc) to your file templates.

Applies to: NetBeans 6.0+

Platforms: All

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo