WebStorm 2021.2 Help

File templates文件模板

File templates are specifications of the default contents for new files that you create. 文件模板是您创建的新文件的默认内容的规范。Depending on the type of file you are creating, templates provide initial code and formatting expected in all files of that type (according to industry or language standards, your corporate policy, or for other reasons).根据要创建的文件类型,模板提供该类型的所有文件所需的初始代码和格式(根据行业或语言标准、公司政策或其他原因)。

WebStorm provides predefined templates for all supported file types suggested when you create a new file.WebStorm为创建新文件时建议的所有支持的文件类型提供预定义模板。

To manage and configure file templates, open the Editor | File and Code Templates page of the IDE settings Ctrl+Alt+S.要管理和配置文件模板,请打开IDE设置Ctrl+Alt+S的“编辑器”|“文件和代码模板”页面。

  • The Default scope controls templates that apply to the entire workspace in any project. 默认范围控制应用于任何项目中整个工作区的模板。They are stored in the IDE configuration directory under fileTemplates.它们存储在fileTemplates下的IDE配置目录中。

  • The Project scope controls templates that apply only to the current project. 项目范围控制仅适用于当前项目的模板。They are stored in the project folder under .idea/fileTemplates. 它们存储在.idea/fileTemplates下的项目文件夹中。These templates can be shared among team members.这些模板可以在团队成员之间共享。

By default, the list of templates contains only predefined templates provided by WebStorm. 默认情况下,模板列表仅包含WebStorm提供的预定义模板。Some of them are internal, which means they cannot be deleted or renamed. 其中一些是内部的,这意味着它们不能被删除或重命名。The names of internal templates are shown in bold. The names of templates that you modified, as well as custom templates that you created manually, are shown in blue.内部模板的名称以粗体显示。您修改的模板以及手动创建的自定义模板的名称以蓝色显示。

WebStorm uses several types of templates, separated into the following tabs:WebStorm使用几种类型的模板,分为以下选项卡:

  • The Files tab contains file templates for creating new files.文件”选项卡包含用于创建新文件的文件模板。

  • The Includes tab contains pieces of reusable content for inserting into file templates (for example, if a specific header applies to multiple file templates).包含”选项卡包含用于插入文件模板的可重用内容(例如,如果特定标题应用于多个文件模板)。

  • The Code tab contains internal templates for code fragments (snippets) used by WebStorm to generate various constructs. 代码”选项卡包含WebStorm用于生成各种构造的代码片段(片段)的内部模板。You can edit the available snippets on this tab, but you cannot create new ones.您可以编辑此选项卡上的可用代码段,但不能创建新代码段。

    To create custom code snippets, use Live templates.要创建自定义代码段,请使用活动模板

The following procedures describe how to create file templates. 以下过程描述了如何创建文件模板。Similar procedures can be used for creating include templates.类似的过程可用于创建包含模板

Create a new file template创建新的文件模板

  1. Press Ctrl+Alt+S to open IDE settings and select Editor | File and Code Templates.Ctrl+Alt+S打开IDE设置并选择“编辑器”|“文件和代码模板”。
  2. On the Files tab, click the Create Template button and specify the name, file extension, and body of the template.在“文件”选项卡上,单击the Create Template button并指定模板的名称、文件扩展名和正文。

  3. Apply the changes and close the dialog.应用更改并关闭对话框。

Copy an existing file template复制现有文件模板

  1. Press Ctrl+Alt+S to open IDE settings and select Editor | File and Code Templates.Ctrl+Alt+S打开IDE设置并选择“编辑器”|“文件和代码模板”。
  2. On the Files tab, click the Copy Template button and modify the name, file extension, and body of the template as necessary.在“文件”选项卡上,单击the Copy Template button并根据需要修改模板的名称、文件扩展名和正文。

  3. Apply the changes and close the dialog.应用更改并关闭对话框。

Save a file as a template将文件另存为模板

  1. Open a file in the editor.在编辑器中打开一个文件。

  2. From the main menu, select File | Save File as Template.从主菜单中,选择“文件”|“将文件另存为模板”。

  3. In the Save File as Template dialog, specify the new template name and edit the body, if necessary.在“文件另存为模板”对话框中,指定新模板名称,并根据需要编辑主体。

  4. Apply the changes and close the dialog.应用更改并关闭对话框。

Syntax语法

File and code templates use the Velocity Template Language (VTL), which includes the following constructs:ile和代码模板使用Velocity模板语言(VTL),其中包括以下结构:

  • Fixed text (markup, code, comments, and so on), which is rendered as-is.固定文本(标记、代码、注释等),按原样呈现。

  • Variables, which are replaced by their values.变量,由其值替换。

  • Various directives, including #parse, #set, #if, and others.各种指令,包括#parse#set#if和其他指令。

Fore more information, see the VTL reference guide.有关更多信息,请参阅VTL参考指南

The following example shows the default template for creating a JavaScript class in WebStorm:以下示例显示了在WebStorm中创建JavaScript类的默认模板:

/** * Created by ${USER} on ${DATE} */

In this template, ${USER} and ${DATE} are template variables.在此模板中,${USER}${DATE}是模板变量。

When you create a new JavaScript file, this template generates a file with contents similar to the following:创建新的JavaScript文件时,此模板将生成一个包含以下内容的文件:

/** * Created by John.Smith on 6/7/11 */

The following example shows the default template for creating a Gherkin features file in WebStorm:以下示例显示了在WebStorm中创建小黄瓜特征文件的默认模板:

# Created by ${USER} at ${DATE} Feature: #Enter feature name here # Enter feature description here Scenario: # Enter scenario name here # Enter steps here

In this template, ${USER} and ${DATE} are template variables. When you create a new Gherkin file, this template generates a feature file with contents similar to the following:在此模板中,${USER}${DATE}是模板变量。创建新的小黄瓜文件时,此模板将生成一个要素文件,其内容类似于以下内容:

# Created by jetbrains at 25.06.2018 Feature: #Enter feature name here # Enter feature description here Scenario: # Enter scenario name here # Enter steps here
Last modified: 21 July 2021