PyCharm 2024.2 Help

Templates with multiple files

Some programming patterns and frameworks require a set of related files, usually with a very specific structure.

In PyCharm, 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.

Create a template with multiple files

  1. In the Settings dialog (Ctrl+Alt+S) , select Editor | File and Code Templates.

  2. Create the main file template.

    On the Files tab, click the Create Template button and specify the name, file extension, and body of the template.

  3. Select the new template in the list and click The Create Child Template File button on the toolbar. Specify the name, file extension, and body of the child template.

Example: create test and scenario files

Consider creating a BDD test based on a scenario recorded in a Gherkin feature file. This tutorial shows how you can add a template to create both files at once.

  1. In the Settings dialog (Ctrl+Alt+S) , select Editor | File and Code Templates.

  2. Create the test template.

    On the Files tab, click the Create Template button and specify the following:

    • Name: Test Scenario

    • Extension: py

    • File name: ${NAME}Test

    The resulting test file will have the provided name with Test appended to it, such as BDDTest.

  3. Click Apply to add the template.

  4. Create the Gherkin feature template.

    Select the created Test Scenario template in the list and click The Create Child Template File button in the toolbar. Specify the following:

    • File name: tests/test

    • Extension: feature

    The resulting feature file will have the name test.feature. It will be stored in the tests.

  5. Click OK to apply the changes.

  6. To use the new template, right-click a directory in the Project tool window or press Alt+Insert, select the Test Scenario template, and specify a name for the controller class. PyCharm will create both the controller class and Twig template files.

Example of using Live Templates
Last modified: 22 October 2024