Templates

Templates provide a convenient method to insert a pre-defined block of text into a program.

The following table describes the template options on the Preferences → IDL → Templates preferences page:

Option
Description

Template List

This list displays the templates currently loaded in the IDL Workbench.

When you select a template in the list, the template contents are displayed in the Preview box.

If the value in the Auto Insert column is "on", the template is automatically inserted if there is an exact content assist match.

New/Edit

Creates a new template, or edits a template selected from the list.

Refer to Creating or Modifying a Template for more information.

Remove

Removes a selected template from the list.

Restore Removed

Restores all the current templates removed from the list.

Revert to Default

If you altered a template, this option restores the original version of the selected template.

Import

Imports an external .xml file containing IDL Workbench templates. A template file can be created using the Export button.

Export

Exports one or more selected templates into an external .xml file. These files can later be reimported with the Import button.

Preview

The Preview box shows the contents of a selected template.

Restore Defaults

Restores the Templates page to its initial configuration.

Apply

Applies all the current preference changes made to the Templates page.

Creating or Modifying a Template

In addition to using preexisting templates, you can create your own code templates and modify them later.

To create a new template or modify an existing template:

  1. On the Preferences → IDL → Templates page, click New or select a template from the Template list and click Edit.
  2. The New Template or Edit Template dialog appears.

  3. Enter a template name in the Name box, or modify the existing name.
  4. Select Automatically insert to ensure that templates that exactly match a content assist request are automatically inserted into the editor.
  5. Note
    The Automatically insert option is only useful for templates that do not include a line or word selection.

  6. Describe the template in the Description box.
  7. Enter or modify the template text in the Pattern box.
  8. You can insert variables into your template, which allow you to customize a template upon insertion. Variable names follow the form:

    ${variable_name} 
     

    Once a template is inserted in the editor, the variable sections of the template are active, and the variable text can be changed.

  9. Click Insert Variable to insert a predefined variable into the template.
  10. Refer to Predefined Variables for a description of the predefined template variables.

  11. Click OK.
  12. If you changed the name of the existing template, you are presented with the option of either renaming the template, or creating another template with the new name.

Predefined Variables

The following table describes the predefined variables available when constructing a template.

Predefined Variable
Description

cursor

The cursor position after editing template variables. This is the spot in the template for adding additional code after the template is inserted into the editor.

date

The current date value.

dollar

The dollar character ( `$' ).

Normally, the `$' character is the beginning character of a user-defined variable. This variable is used when you need to insert a `$' character into your code, but do not want it to be interpreted as a user-defined variable.

line_selection

The lines selected in the editor before the template is inserted.

time

The current time.

user

The current process user name.

word_selection

The word selected in the editor before the template is inserted.

year

The current year.

Inserting a Template into an Editor

If a template contains a word selection or a line selection variable, you must select the appropriate text prior to inserting the template. Selecting text prior to invoking the content assist menu also means that you must explicitly choose the template from the list (automatic template insertion is not an option).

To insert a template into your code that does not include a line or word selection:

  1. Type the name (or part of a name) of a template in the editor.
  2. Press Ctrl-Space to display the list of content assist items.
  3. Templates have the template_icon.gif icon.

    If the template was configured to automatically insert itself, and if the name of the template does not match any other content assist item, the template is inserted automatically into the editor.

  4. Select a template from the list to display the template's content.
  5. Double-click a template in the list (or press Enter) to insert the template into the editor.
  6. If the template contains user-defined variables, the keyboard focus is on the first variable. Edit the variable value, and press Tab to highlight the next variable (if one exists).
  7. After the last variable, if the template contains a ${cursor} variable, pressing Tab places the keyboard focus at the ${cursor} position. Enter additional code at this position.

To insert a template into your code that includes a line or word selection:

  1. In the editor, select the desired text.
  2. Press Ctrl-Space to display the list of content assist items.
  3. Templates have the template_icon.gif icon.

  4. Select a template from the list to display the template's content.
  5. Double-click a template in the list (or press Enter) to insert the template into the editor.
  6. If the template contains user-defined variables, the keyboard focus is on the first variable. Edit the variable value, and press Tab to highlight the next variable (if one exists).
  7. After the last variable, if the template contains a ${cursor} variable, pressing Tab places the keyboard focus at the ${cursor} position. Enter additional code at this position.

Inserting a Template into the Command Line View

A template that consists of a single line can be inserted into the Command Line view as well as into an editor.

For example, suppose you find yourself wanting to select a data file with the extension .dat from a specific directory quite often. You could define a one-line template similar to the following, giving it a short name like df:

${file}=DIALOG_PICKFILE(PATH='C:\Sensor\Data', FILTER='*.dat') 

Then when you want to select a data file, simply type df at in the Command Line view and press Ctrl-Space. Assuming no other template begins with the characters df, the template is inserted into the Command Line with the initial file highlighted, allowing you to quickly change the variable name. Pressing Return brings up the file selection dialog in the specified directory, with the file list filtered for .dat files.