Web-specific file templates
File templates are specifications of the default contents for new files that you create. Depending on the type of file you are creating, templates provide initial code and formatting expected in all files of that type (according to industry or language standards, your corporate policy, or for other reasons).
JetBrains Rider provides predefined templates for all supported file types suggested when you create a new file.
Manage and configure file templates
Press Ctrl+Alt+S to open the IDE settings and then select
.
For more information, refer to Other Languages.
By default, the list of templates contains only predefined templates provided by JetBrains Rider. Some of them are internal, which means they cannot be deleted or renamed. JetBrains Rider shows the names of internal templates in bold. The names of templates that you modified, as well as custom templates that you created manually, are shown in blue.
The following procedures describe how to create file templates. Similar procedures can be used for creating include templates.
Create a new file template
Press Ctrl+Alt+S to open the IDE settings and then select
.Using the Scheme list, select the scope to which the file template applies:
Default: configure file templates for the entire application. These templates are available in all projects that you open with the current IDE instance. Use them as your personal templates that you prefer regardless of the specific project. JetBrains Rider stores global templates in the IDE configuration directory under fileTemplates.
Project: configure file templates specific for the current project. These templates are available to everyone who works on this project. JetBrains Rider stores them in the project folder under .idea/fileTemplates.
On the Files tab, click and specify the template name, file extension, name of the resulting file, and body of the template.
Apply the changes and close the dialog.
Copy an existing file template
Press Ctrl+Alt+S to open the IDE settings and then select
.On the Files tab, click and modify the name, file extension, and body of the template as necessary.
Apply the changes and close the dialog.
Save a file as a template
Open a file in the editor.
In the main menu, go to
.In the Save File as Template dialog, specify the new template name, extension, and edit the body if necessary. For more information about the syntax used in file templates, see Syntax
File name: If necessary, specify a name for the file created from this template. By default, JetBrains Rider prompts the user to enter a name when adding the file. You can hard-code a specific name to avoid the prompt entirely, or compose a template from available variables. For example, here is how you can use the specified name but put it one directory above the one that was selected during file creation:
../${NAME}
Reformat according to style: Reformat the contents generated based on this template according to the code style defined for this file type.
Enable Live Templates: Insert live templates inside the file template. Use the Velocity escape syntax to include live template variables in a file template, for example:
#[[ $MY_VARIABLE$ $END$ ]]#
Apply the changes and close the dialog.
Syntax
File templates use the Velocity Template Language (VTL), which includes the following constructs:
Plain text rendered as is.
Variables that are replaced by their values. For example,
${NAME}
inserts the name provided by the user when adding the file.Various directives, including #parse,
#set
,#if
, and others.
Start typing $
or #
to refer to completion suggestions for available variables and directives.
For more information, refer to the VTL reference guide.
The following example shows the default template for creating a JavaScript class in JetBrains Rider:
In this template, ${USER}
and ${DATE}
are template variables.
When you create a new JavaScript file, this template generates a file with contents similar to the following: