# File Templates

## Introduction

The File Templates plug-in allows you to create files based on other files. You can use any file as a template, which will create a copy of the file with an empty &URL;, or use a special template file which may contain macros to fill in information like your name and email address, the current date and so on, and position the cursor at a convenient position in the new file.

Furthermore, templates located in the template folder will be presented in the menu item File New from Template.

The plug-in also provides a method to easily create a new template from an open document.

The template folder is part of the &kde; file system, and consists of at least TDEDIR/share/applications/kate/plugins/katefiletemplates/templates and TDEHOME/share/applications/kate/plugins/katefiletemplates/templates. If your TDEDIRS environment variable contains additional directories, those are searched for a similar subdirectory as well. If equally named templates are found, the one in the local (TDEHOME) folder is chosen.

## Menu Structure

File New From Template Any File...

- Presents you with Open File dialog that allows you to use any file as a template. If the chosen file has the extension `katetemplate` it will be parsed for template information and macros.

File New From Template Use Recent

- Presents a list of files recently used as templates, represented by their &URL;.

File New From Template

- The remainder of submenus contains links to templates. Click a menuitem to create a file as described by the menu item text.

Settings Manage Templates...

- This will launch a dialog with a list of all templates found within the template directories, along with options to add, edit or remove templates.

## Using a &kate; Template

When creating a file from a template that contain template macros, some macros appears as editable variables in the text. Such variables appears as underlined words in the text.

The first variable will be selected, so you just have to type to edit it.If the document text contains more instances of the same variable, they are changed as you edit. To move to the next editable variable, press the TAB key. When the last variable is edited, the list is dropped, and your TAB key works as normal.

## Creating your own templates

To create a new template, use the Settings Manage Templates Item to launch the template management dialog. In that, click New... to launch the File Template Wizard. You will be asked for an optional file to turn into a template and prompted for template information settings, and a template file will be created for you.

Alternatively, you can create a template manually by adding template information to the top of any file, add text and macros, and save it with the `katetemplate` extension.

The template menu gets automatically updated if you chose to store your template in the template directory.

## Editing templates

To edit a template, use the Settings Manage Templates.... Select the template you want to work on and click Edit..., and the template file will be opened. Close the dialog, edit the template file as desired, save it and close it. Changes to templates takes immediate effect, you can activate the template to test your changes after saving it.

## The &kate; Template Format

If you use files with the extension `katetemplate`, they will be parsed for template information, macros and a cursor position.

### Template information

While reading in the file, the parser keeps lines beginning with the phrase `katetemplate:` and searches them for template information in the form VARIABLENAME=VALUE. The first line not starting with `katetemplate:` will be taken as the start of the template contents. VALUE may contain any character but equal sign (=). Legal variable names are:

`Template`

- This is the template name, displayed in the File New from Template menu.

`Group`

- The group places the template in a submenu of the File New from Template menu.

`Name`

- This is the name that will be set for the document, and displayed in the file list and title bar. If the name contains `%N` that will be replaced with a number, increasing if more documents has the same name.

`Highlight`

- The plug-in will try to set the Highlight for the new document to the value of this variable. The value should be the name, as found in the Tools Highlighting .

`Description`

- A short informative description of the template. This is currently used to set a Whatsthis string for the menu item, but may be used for more purposes in the future.

Author

- A string identifying the author, for example in the form `Name <email address>`. This is currently used to set a Whatsthis string for the menu item, but may be used for more purposes in the future.

### Template Macros

While parsing the template contents, macros in the form `%{NAME}` or `${NAME}` are expanded. If you use the `$` prefix, the expanded macro will be treated as a editable variable when a document is created from the template, whereas if you use `%` it is not, unless expanding failed.

The following macros are expanded:

time

- Expands to the current time in your locale format.

date

- Expands to the current date in short format.

datetime

- Expands to the current date and time, formatted as a string according to your locale.

year

- The current year as a four digit number.

month

- The full name of the current month, according to your locale.

day

- Expands to the current day of the month.

hostname

- Expands to the 'hostname' of your computer.

index

- Expands to 'i'.

fullname

- Expands to your full name, as defined by the owner addressee in your standard &kde; addressbook.

firstname

- Expands to your first name, as defined in the owner addressee in your standard &kde; addressbook.

lastname

- Expands to your last name, as defined in the owner addressee in your standard &kde; addressbook.

email

- Expands to your email address, as defined by the owner address in your standard &kde; addressbook.

Any macro not in the above list is treated as a editable variable no matter the prefix. If the same variable occurs multiple times in the template, they can be edited at once after creating a document from the template.

### Setting the cursor position

The special macro `${cursor}` will be replaced with a vertical bar and added to the end of the list of editable variables, independent on its location in the text.

## Thanks and Acknowledgments

&kate; Plug-in File Templates copyright 2004 &Anders.Lund; &Anders.Lund.mail;.

Documentation copyright 2004 &Anders.Lund;

&underGPL;