Templates with multiple files
Some programming patterns and frameworks require a set of related files, usually with a very specific structure. For example, with the model-view-controller (MVC) pattern, you need separate files for the model, view, and controller.
In PhpStorm, you can create sets of related files by adding child templates to a file template. When you create a file from such a template, it will also create files from child templates.
In the Settings dialog () , select Editor | File and Code Templates.
Create the main file template.
On the Files tab, click and specify the name, file extension, and body of the template.
Select the new template in the list and click on the toolbar. Specify the name, file extension, and body of the child template.
note
All child templates share the variables of the main file template.
You cannot add child templates to the default file templates.
Let's say you want to create a Symfony Controller and the associated Twig Template. This tutorial shows how you can add a template to create both files at once.
tip
For more information about Symfony support in PhpStorm, refer to Symfony.
In the Settings dialog () , select Editor | File and Code Templates.
Create the Controller class template.
On the Files tab, click and specify the following:
Name:
Symfony Controller
Extension:
php
File name:
${NAME}Controller
The resulting controller class will have the provided name with
Controller
appended to it, such asExampleController
.Create the Twig template.
Select the created Symfony Controller template in the list and click in the toolbar. Specify the following:
File name:
../../templates/${NAME.toLowerCase()}/index.html
Extension:
twig
The resulting Twig Template file will have the name index.html.twig. It will be stored in a subfolder under templates, whose name will correspond to the controller class.
tip
In this example, the path to the templates folder is provided relative to the location of a controller class as follows: ..
/.. . Alternatively, you can provide the path relative to the project root by starting it with/templates/ /
as follows:/templates/ .Click OK to apply the changes.
To use the new template, right-click a directory in the Project tool window or press , select the Symfony Controller template, and specify a name for the controller class. PhpStorm will create both the controller class and Twig template files.