File template variable

The content of the article is reproduced from the official help document of IDEA: File template variables | IntelliJ IDEA

Translated by Google

Predefined template variables

The following predefined variables are available for file templates:

volatile

describe

${DATE}

Current system date

${DAY}

Current date of each month

${DIR_PATH}

From content root Directory to the new file directory

${DS}

Dollar sign $. This variable is used to escape dollar characters so that they are not treated as prefixes to template variables.

${FILE_NAME}

The name of the new PHP file, if enabled PHP plugin)

${HOUR}

current time

${MINUTE}

Current minute

${SECOND}

Current second

${MONTH}

This month

${MONTH_NAME_FULL}

Full name of the month (January, February, etc.)

${MONTH_NAME_SHORT}

The first three letters of the name of the current month (Jan, Feb, etc.)

${NAME}

Name of the new entity (file,% class%, interface, etc.)

${PACKAGE_NAME}

The name of the target package that created the new class or interface file

${PRODUCT_NAME}

The name of the IDE (for example, IntelliJ IDEA)

${PROJECT_NAME}

Current project name

${TIME}

Current system time

${USER}

Login name of the current user

${YEAR}

this year

Custom template variables

In addition to predefined template variables, you can also specify custom variables# set if necessary, you can use the directive to define the value of the custom variable in the template. Write instructions before using the corresponding variables.

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

<span style="background-color:var(--wh-color-backlight-main)"><span style="color:var(--wh-color-text-main)"><code>#set( $MyName = "John Smith" )</code></span></span>

If the value of the variable is not defined in the template, IntelliJ IDEA will ask you to specify it when applying the template.

For example, you can create the following Java file template, which will prompt you for the name of the method and the parameters of the method every time you create a new file based on this template:

<span style="background-color:var(--wh-color-backlight-main)"><span style="color:var(--wh-color-text-main)"><code><strong><span style="color:#0077aa">#if</span></strong> <span style="color:#999999">(</span><span style="color:#ee9900">$<span style="color:#999999">{</span>PACKAGE_NAME<span style="color:#999999">}</span></span> <span style="color:#9a6e3a">!=</span> <strong><span style="color:#669900">""</span></strong><span style="color:#999999">)</span>package <span style="color:#ee9900">$<span style="color:#999999">{</span>PACKAGE_NAME<span style="color:#999999">}</span></span>;<strong><span style="color:#0077aa">#end</span></strong>

public class <span style="color:#ee9900">$<span style="color:#999999">{</span>NAME<span style="color:#999999">}</span></span> {
    static void <span style="color:#ee9900">$<span style="color:#999999">{</span>MethodName<span style="color:#999999">}</span></span>(String <span style="color:#ee9900">$<span style="color:#999999">{</span>Parameter<span style="color:#999999">}</span></span>) {
        System.out.println(<span style="color:#ee9900">$<span style="color:#999999">{</span>Parameter<span style="color:#999999">}</span></span>);
    }
}</code></span></span>

In this example, we use ${MethodName}and${Parameter} variables without setting their values. When you create a file based on this template, IntelliJ IDEA will require values for these variables:

In the generated file, the variable is replaced with the value you entered in the new file dialog box (method and parameter names in this example):

<span style="background-color:var(--wh-color-backlight-main)"><span style="color:var(--wh-color-text-main)"><code><strong><span style="color:#0077aa">package</span></strong> com<span style="color:#999999">.</span>example<span style="color:#999999">.</span>somepackage<span style="color:#999999">;</span>

<strong><span style="color:#0077aa">public</span></strong> <strong><span style="color:#0077aa">class</span></strong> newClass <span style="color:#999999">{</span>
    <strong><span style="color:#0077aa">static</span></strong> <strong><span style="color:#0077aa">void</span></strong> <span style="color:#dd4a68">newMethod</span><span style="color:#999999">(</span><span style="color:#dd4a68">String</span> myName<span style="color:#999999">)</span> <span style="color:#999999">{</span>
        <span style="color:#dd4a68">System</span><span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#dd4a68">println</span><span style="color:#999999">(</span>myName<span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
<span style="color:#999999">}</span></code></span></span>

 

Keywords: Java IntelliJ IDEA intellij-idea

Added by poppy on Mon, 14 Feb 2022 11:23:08 +0200