Code Template Syntax

You can use code templates to speed up the entry of commonly used sequences of reserved words and common code patterns. For example, if you enter forc and press the Tab key, it expands into the following:

  for (Iterator it = col.iterator(); it.hasNext();) {
Object object = it.next();

When you create code templates, there are several constructs that you can use to customize the way the code template behaves. You can look at the default IDE code templates in the Options window, under the Tools menu, for examples from which you can learn.

In general, a code template parameter can be specified by its name and a set of optional hints. Hints serve as guidance when computing the values assigned by the infrastructure to the parameters on template expansion. Syntax for parameter definition is as follows:

${param_name hint=value hint=value ...}

However, boolean hints that can be written without the value part:

${param_name hint} translates to ${param_name hint=true}

Reserved Parameter and Hint Names

Some parameter names are reserved by the code template infrastructure:

Similarly, some of the hint names are reserved by the code template infrastructure:

Pre-defined Parameters

NetBeans comes with a number of pre-defined parameters that you can use when creating code templates. You can add parameter definitions in the Template Manager. Note also special rules for changing the the format of the ${date} variable.

Adding Parameter Definitions

You can define additional parameters in the Template Manager.

To define a parameter:

  1. In the IDE's menu bar, choose Tools > Templates. The Template Manager opens.
  2. Expand the Other category. Double-click on Properties. The User.properties file opens in the editor.
  3. Define a parameter. For example, define the ${user} parameter for adding your name to templates you create. The syntax is
    user=LorumIpsum@mycompany.com

    When a code template is expanded, all occurrences of ${user} in that template are replaced with 'LorumIpsum@mycompany.com'.

  4. Add as many additional parameters as you want, with the syntax name=value.

Parameter definition follows the FreeMarker Java template language. For more information, see the .

Formatting the ${date} Parameter

The ${date} variable is of String type, not Date type. You have to cast ${date} to a Date type in order to change its format. For example, to show the year only, add the following parameter to your code template:

${date?date?string("yyyy")}

Legal Notices