PhpStorm FileTemplatesinPhpStorm 200116 0033 2502

download PhpStorm FileTemplatesinPhpStorm 200116 0033 2502

of 7

description

File Templates in PhpStorm

Transcript of PhpStorm FileTemplatesinPhpStorm 200116 0033 2502

  • File Templates in PhpStorm

    File and Code TemplatesTemplatesIncludesCode

    Creating and Modifying File and Code TemplatesUsing Velocity constructs in a TemplatePrompting for Input when using a Template

    File and Code TemplatesPhpStorm ships with a number of File Templates. We can find them under . We can see the templatesIDE Settings | File and Code Templatesthat are available: there are templates for HTML files, HTML5, JavaScript, TypeScript and CoffeeScript, PHP and so on. I've also added one ofmyself: for generating pages using the .Smarty Page Smarty templating engine

    Tweet

    When creating new files in PhpStorm, for example using the context menu, PhpStorm typically generates some defaultNew | PHP Filecontents for that file, for example a copyright header. This content is generated by File Templates. In this tutorial, we will see what these areand how we can use them in our own projects.

    Templates can be created for any language, even languages not supported by the IDE. If it should be easy to add a file toreadme.txtour projects, we could create a File Template for that. If for some reason we would need to be able to quickly add an file inauthors.xmlwhich our current username should be stored, we could do so too.

  • There are several tabs available: , _ Includes_ and . These different tabs provide access to the different types of File and CodeTemplates CodeTemplates that are available and can be extended. Let's see what these types all mean.

    TemplatesThe tab contains the various file templates that are available when using the context menu (or / onTemplates New | PHP File Alt+Insert CMD+NMac OS X). They all generate a complete file and can contain plain text, (using the directive) and special variables that will be#Includes #parsesubstituted in the resulting file. We'll see what these are in the section of this tutorial.#Creating and Modifying File and Code Templates

  • When we select a template, we can see its contents.The template, for example, contains nothing but a PHP open tag and renders the PHP File P .HP File Header.php #Include

  • IncludesThe tab contains partial templates. An can not be generated on its own, but can be used and re-used in various templates. TheyIncludes includecan contain plain text, other (using the directive) and special variables that will be substituted in the resulting file. We'll see what#Includes #parsethese are in the section of this tutorial.#Creating and Modifying File and Code Templates

    CodeThe tab contains a special kind of templates. In the section of the Code Generating Code Working with the PhpStorm Editor Actions and

    tutorial, we've seen that we can generate code the editor, for example getters and setters in a PHP class. While we can notNavigation withincreate new code templates, we could alter them so code is always generated the way we like it.

  • Code templates can contain plain text, (using the directive) and special variables that will be substituted in the resulting file.#Includes #parseWe'll see what these are in the section of this tutorial. When selecting a code template, we can#Creating and Modifying File and Code Templatessee a number of additional pre-defined variables are available for use in our template.

    Creating and Modifying File and Code TemplatesPhpStorm uses the template language for File and Code templates, which supports variousVelocity Template Language (VTL or just "Velocity")constructs. We can add fixed text like markup, code and comments. We can use file template variables such as or ${PROJECT_NAME} ${FILE_

    that will be replaced in the generated file with our project's name or the file name. Check the for a full list of available variablesNAME} web helpfor the various templates.

    Let's update one of the . Open the and click the tab. We want to change the #Includes IDE Settings | File and Code Templates Includes PHP that is being generated at the start of every PHP file that is generated. We can edit the template directly on the right.File Header

    Instead of the default, let's change the template to:PHP File Header

    /** * ${PROJECT_NAME} - ${FILE_NAME} *

    * Initial version by: ${USER} * Initial version created on: ${DATE} */

    One character we may need in our PHP templates is the dollar sign ($). Since the Velocity template language also used the dollar signto denote a variable, we have to somehow escape it in our templates. We can use the variable instead of just in our PHP${DS} $templates (or other templates where we need a $ printed).

    If a backup of a template is needed, we could use the button from the toolbar to create a copy first.Copy Template

  • When we save the template and use context menu (or New | PHP File Alt+Ins / on Mac OS X) and pick the or template, theert CMD+N PHP File PHP Class

    file that is generated now contains our modified header.

    Using Velocity constructs in a TemplateSo far, we have seen our templates can contain plain text, (using the directive) and variables that will be substituted in the#Includes #parseresulting file. The Velocity Template Language supports other constructs like , , etc. as well. A good example can be seen in the if foreach PHP

    file template: when a namespace is defined, the generated file will contain a PHP namespace declaration. If not, it will be omitted.Class

  • { "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: will populate the template with the current username, will populate${USER} ${PROJECT_NAME}the template with the current project name. However, where will , and come from?${Description} ${Author} ${AuthorEmail}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

    File Templates in PhpStorm