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 and custom variables are available in the template body and in the filename. For example, if you type MyFile_${MONTH_NAME_FULL}
in the File name field of your template, the resulting file will be named MyFile_April
if you create it in April. Here, ${MONTH_NAME_FULL}
is a predefined variable. If you use a custom variable instead, PhpStorm will prompt you to enter the variable value when you create a file based on this template.
Predefined template variables
The following predefined variables can be used in file templates:
Variable | Description |
---|---|
| Current system date |
| Current day of the month |
| Dollar sign |
| Name of the new PHP file |
| Current hour |
| Current minute |
| Current second |
| Current month |
| Full name of the current month (January, February, and so on) |
| First three letters of the current month name (Jan, Feb, and so on) |
| Name of the new entity (file, class, interface, and so on) |
| Fully qualified name (without a leading slash) of the class or field namespace |
| Name of the IDE (for example, PhpStorm) |
| Name of the current project |
| Current system time |
| Login name of the current user |
| Current year |
PhpStorm provides a set of additional variables for PHP include templates, which are used to define reusable pieces of code, such as file headers and PHPDoc comments.
The following additional predefined variables can be used in PHP include templates:
Variable | Description |
---|---|
| Caret position after you finish editing the variables. Applied only when a PHPDoc comment is generated and inserted during file creation. When a PHPDoc comment is created through |
| Name of the class where the field or method to generate the PHPDoc comment for is defined |
| Name of the class, field, or function (method) for which the PHPDoc comment will be generated |
| Fully qualified name (without a leading slash) of the class or field namespace |
| Documentation comment for parameters. Evaluates to a set of |
| Is replaced by the |
| Documentation comment for exceptions. Evaluates to a set of |
| Return value of the function (method) to generate the PHPDoc comment for. If the return type cannot be detected through static analysis of the function (method), evaluates to |
Variable methods
Because PhpStorm 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:
If the value of a variable is not defined in the template, PhpStorm will ask you to specify it when the template is applied.