DataGrip 2024.2 Help

File template variables

A file template can contain variables, which are replaced by their values when the template is applied. A variable is a string that starts with a dollar sign $ followed by the variable name. The variable name may optionally be enclosed in curly braces. For example: $MyVariable and ${MyVariable} are different notations of the same variable.

Predefined template variables

The following predefined variables can be used in file templates:

Variable

Description

${DATE}

Current system date

${DAY}

Current day of the month

${DS}

Dollar sign $. This variable is used to escape the dollar character, so that it is not treated as a prefix of a template variable.

${FILE_NAME}

Name of the new file

${HOUR}

Current hour

${MINUTE}

Current minute

${SECOND}

Current second

${MONTH}

Current month

${MONTH_NAME_FULL}

Full name of the current month (January, February, and so on)

${MONTH_NAME_SHORT}

First three letters of the current month name (Jan, Feb, and so on)

${NAME}

Name of the new entity (file, class, interface, and so on)

${PRODUCT_NAME}

Name of the IDE (for example, DataGrip)

${PROJECT_NAME}

Name of the current project

${TIME}

Current system time

${USER}

Login name of the current user

${YEAR}

Current year

Variable methods

Because DataGrip uses Velocity as a template engine for file templates, variables in file templates can use Java String methods. For example, the following is possible:

  • ${NAME.toUpperCase()}: convert the name of the new entity (file) to upper case letters.

  • ${PROJECT_NAME.length()}: print the length of the project name.

  • ${PRODUCT_NAME.substring(0,5)}: print the first five characters of the name of the IDE you are using.

Custom template variables

Besides predefined template variables, it is possible to specify custom variables. If necessary, you can define the values of custom variables right in the template using the #set directive. Write the directive before the corresponding variable is used.

For example, if you want to use your full name instead of your login name defined through the predefined variable ${USER}, add the following construct before your custom variable:

#set( $MyName = "John Smith" )

If the value of a variable is not defined in the template, DataGrip will ask you to specify it when the template is applied.

For example, you can create the following SQL file template that will prompt you to enter the ID and FIRST NAME values each time you create a new file based on this template:

select * from actor; #set( $LAST_NAME = "Smith" ) insert into actor (actor_id, first_name, last_name, last_update) values (${ID},'${FIRST_NAME}','${LAST_NAME}','${DATE} ${TIME}');

In this example, we use the ${ID} and ${FIRST_NAME} variables and do not set their values. When you create a file based on this template, DataGrip will ask you to provide values for these variables:

Creating a file based on a file template

In the resulting file, the variables will be replaced with the values that you entered in the new file dialog (method and parameter names in this example):

select * from actor; insert into actor (actor_id, first_name, last_name, last_update) values (202, 'Peter', 'Smith', '01.02.2023 13:34');

Example

The following example shows a template for creating a user-defined function in DataGrip:

CREATE FUNCTION `${FUNCTION_NAME}`(`$arg1`, `$arg2`, `$arg3`) RETURNS BEGIN RETURN END #parse("Copyright.sql")

When you create a new custom SQL file, this template generates a file with contents similar to the following:

CREATE FUNCTION `functionName`(`arg1`, `arg2`, `arg3`) RETURNS BEGIN RETURN END /** * Copyright. Created by User on 10.08.2018 */
Last modified: 26 May 2024