Professional Documents
Culture Documents
PhpStorm FileTemplatesinPhpStorm 200116 0033 2502
PhpStorm FileTemplatesinPhpStorm 200116 0033 2502
Tweet
When creating new files in PhpStorm, for example using the New | PHP File context menu, PhpStorm typically generates some default
contents for that file, for example a copyright header. This content is generated by File Templates. In this tutorial, we will see what these are
and how we can use them in our own projects.
File and Code Templates
Templates
Includes
Code
Creating and Modifying File and Code Templates
Using Velocity constructs in a Template
Prompting for Input when using a Template
There are several tabs available: Templates, _ Includes_ and Code. These different tabs provide access to the different types of File and Code
Templates that are available and can be extended. Let's see what these types all mean.
Templates
The Templates tab contains the various file templates that are available when using the New | PHP File context menu (or Alt+Insert / CMD+N on
Mac OS X). They all generate a complete file and can contain plain text, #Includes (using the #parse directive) and special variables that will be
substituted in the resulting file. We'll see what these are in the #Creating and Modifying File and Code Templates section of this tutorial.
When we select a template, we can see its contents.The PHP File template, for example, contains nothing but a PHP open tag and renders the P
HP File Header.php #Include.
Includes
The Includes tab contains partial templates. An include can not be generated on its own, but can be used and re-used in various templates. They
can contain plain text, other #Includes (using the #parse directive) and special variables that will be substituted in the resulting file. We'll see what
these are in the #Creating and Modifying File and Code Templates section of this tutorial.
Code
The Code tab contains a special kind of templates. In the Generating Code section of the Working with the PhpStorm Editor Actions and
Navigation tutorial, we've seen that we can generate code within the editor, for example getters and setters in a PHP class. While we can not
create new code templates, we could alter them so code is always generated the way we like it.
Code templates can contain plain text, #Includes (using the #parse directive) and special variables that will be substituted in the resulting file.
We'll see what these are in the #Creating and Modifying File and Code Templates section of this tutorial. When selecting a code template, we can
see a number of additional pre-defined variables are available for use in our template.
/**
* ${PROJECT_NAME} - ${FILE_NAME}
*
* Initial version by: ${USER}
* Initial version created on: ${DATE}
*/
When we save the template and use New | PHP File context menu (or Alt+Ins
ert / CMD+N on Mac OS X) and pick the PHP File or PHP Class template, the
file that is generated now contains our modified header.
Creating File Templates can also be done from within the Editor. After
opening a file we want to use as a File Template, we can use the Tools
| Save File as Template... menu to export it as a File Template.
<?php
#parse("PHP File Header.php")
#if (${NAMESPACE})
namespace ${NAMESPACE};
#end
class ${NAME} {
}
{
"name": "${USER}/${PROJECT_NAME}",
"description": "${Description}",
"minimum-stability": "dev",
"license": "proprietary",
"authors": [
{
"name": "${Author}",
"email": "${AuthorEmail}"
}
]
}
We're using some of the pre-defined variables: ${USER} will populate the template with the current username, ${PROJECT_NAME} will populate
the template with the current project name. However, where will ${Description}, ${Author} and ${AuthorEmail} come from?
When using a template that contains unknown variables, PhpStorm will prompt for them when creating a file based on the template.
The file that is generated using the template will contain the values that were entered by the user.
Tweet