AppCode 2019.3 Help

File and code templates

File templates are specifications of the default contents to be generated when creating a new file. Depending on the type of file you are creating, templates provide initial code and formatting that is expected to be in all files of that type (according to industry standards, your corporate policy, or for other reasons).

AppCode provides predefined templates for all supported file types, which are suggested when you are creating a new file (⌃⌥N).

File templates are managed on the Editor | File and Code Templates page of AppCode preferences ⌘,. This settings page can be configured in one of two scopes:

  • The Default scope controls templates that pertain to the entire workspace. They are stored in the IDE configuration directory under fileTemplates.

  • The Project scope controls templates on a per-project basis. They are stored in the project folder under .idea/fileTemplates. These templates can be shared among team members.

By default, the list of templates contains only predefined templates provided by AppCode. Some of them are internal, which means they cannot be deleted or renamed. The names of internal templates are shown in bold. The names of templates that you modified, as well as custom templates that you created manually, are shown in blue.

AppCode uses several types of templates, separated into the following tabs:

  • The Files tab contains file templates that can be used for creating new files.

  • The Includes tab contains templates of reusable content that can be inserted into file templates (for example, headers).

  • The Code tab contains internal templates for code fragments (snippets) used by AppCode for generating various constructs. You can edit the available snippets on this tab, but you cannot create new ones.

    To create custom code snippets, use Live templates.

The following procedures describe how to create file templates. Similar procedures can be used for creating include templates.

To create a new file template

  1. In the Preferences dialog ⌘,, select Editor | File and Code Templates.

  2. On the Files tab, click the Create Template button and specify the name, file extension, and body of the template.

  3. Apply the changes and close the dialog.

To copy an existing file template

  1. In the Preferences dialog ⌘,, select Editor | File and Code Templates.

  2. On the Files tab, click the Copy Template button and modify the name, file extension, and body of the template as necessary.

  3. Apply the changes and close the dialog.

To save a file as a template

  1. Open a file in the editor.

  2. Choose Tools | Save File as Template from the menu.

  3. In the Save File as Template dialog that opens, specify the new template name and edit the body, if necessary.

  4. Apply the changes and close the dialog.

Syntax

File and code templates are written in the Velocity Template Language (VTL), which enables using the following constructs:

  • Fixed text (markup, code, comments, and so on), which is rendered as-is.

  • Variables, which are replaced by their values.

  • Various directives, including #parse, #set, #if, and others.

Fore more information, see the VTL reference guide.

The following example shows the default template for creating an Objective-C class in AppCode:

#parse("C File Header.h") #import "${HEADER_FILENAME}" @implementation ${NAME} #if ($PUT_IVARS_TO_IMPLEMENTATION == "true"){ } #end @end

This template is organized as follows:

  • The #parse directive is used to insert the contents of the C File Header.h template.

  • The #import directive is used to insert the link to the header file.

  • The template then adds an implementation for a class with the name passed as the ${NAME} value (name of the new file). $PUT_IVARS_TO_IMPLEMENTATION defines, if instance variables should be put inside the class implementation. You can change the value of this option using the Put ivars to implementation if possible checkbox (AppCode | Preferences | Editor | Code Style| C/C++/Objective-C). If the value is true, curly braces will be added after the class name.

When you create a new Objective-C class based on the template, a file with contents similar to the following is generated:

// // Created by John Smith on 2019-07-01. // Copyright (c) 2019 JetBrains. All rights reserved. // #import "NewClass.h" @implementation NewClass { } @end

The corresponding header file is created at the same time.

Last modified: 31 March 2020