Template Name Variables
- 5 minutes to read
This topic introduces the concept of template name variables. It describes how to create a template name variable and use it in templates. The Use Type in Templates section explains how to include a type in type-aware templates.
Template Name Variables are variables that contain the mnemonic list and their values.
The following screenshot shows the “Type” variable with the selected “s” mnemonic and its System.String value on the Editor | All Languages | Template Name Variables options page:
You can include a template name variable in a template to create different template expansions based on one template. See the Create a Template Name Variable and Insert it into a Template section for more information.
For example, the “ms” template (creates a method of the string type) is displayed as “m?Type?” on the Editor | All Languages | Templates options page:
See the following topic for information on how to open the Templates options page: Templates Configuration.
In the “m?Type?” template, the “Type” variable is surrounded by “?“ characters (?variable_name?).
To expand the “m?Type?” template:
Type “m” followed by a mnemonic from the “Name” list for the “Type” variable (see the first screenshot) instead of “?Type?”.
Press Space or Tab depending on your settings.
For example, you can use the following “Type” variable mnemonics: i for integer, d for double, and b for boolean. This allows you to expand mi, md, and mb templates, accordingly. See the following topic for more information on these templates: Members Declaration.
The following screencast shows the “ms” template expansion:
Use Type in Templates
You can create a mnemonic for any type from the code editor’s context menu and use this mnemonic as a template name variable to expand type-aware templates. For example, you can create the “tc” mnemonic for the TestClass type. To expand a template, type “m” (for methods), “p” (for properties), “q” (for constants), or “v” (for variables) followed by this mnemonic.
The screencast below shows the “ptc” template expansion that creates a property of the TestClass type:
Follow the steps below to insert your type in templates:
Right-click a type in the code editor (in this example, “Mixin”), and choose the “Use Type in Templates…” menu item.
In the “Use Type in Templates” dialog, type a mnemonic (for example, “mxn” for the Mixin type) in the corresponding field and press Add.
CodeRush adds the mnemonic and fully-qualified type name to the Template Name Variables options page.
Open this page, choose the Neutral language, and select one of the following groups to see them:
“Custom Types” for non-generic type.
“Custom Generic Types (one param)” for generic type with one parameter.
“Custom Generic Types (two params)” for generic type with two parameters.
CodeRush creates different groups for generic and non-generic types since it uses different variable names for generic types in templates (for example, Generic1Type for generic types with one parameter).
The screenshot below shows the selected “Custom Types” group:
Use the “mxn” mnemonic instead of “?Type?” to expand templates.
Many built-in CodeRush templates use the “Type” variable name created in the “Custom Types” group. For example, “t?Type?”, “m?Type?”, “p?Type?”, “q?Type?”, “v?Type?”, and more.
The screencast below shows the “mmxn” template expansion. This template creates a method of the Mixin type.
Create a Template Name Variable and Insert it into a Template
Follow the steps below to create a template name variable and add it to a template:
Open the Editor | All Languages | Template Name Variables options page.
Click the New Variable Group toolbar button to create a variable group.
CodeRush adds the “NewList” item to the variable groups.
Change the suggested group name, for example, to “Custom Variables”.
Specify the variable name in the corresponding field. You can add a comment for the variable name (optional).
Specify conditions in the “Context” field when the template that contains the template variable name can be expanded (optional). If the “Context” field is empty, you can expand the template in any context.
Execute the following actions to add conditions to the “Context” field:
Click the ellipsis button in the “Context” field.
In the invoked Select Context Provider dialog, choose the context provider and click OK.
Enable the “This list holds .NET types” option if you want to allow CodeRush to simplify the variable values after substitution (for example, the ‘System.Boolean‘ changes to ‘bool‘ in C#).
Add name-value pairs to the list. Use the “+” toolbar button or press Enter in the “Value” field to add a new entry.
Click Apply to save the template name variable.
Open the Templates options page and create a template. See the following topic for more information: How to: Create a Template from Scratch.
Insert a Template Name Variable into your template. Add the variable name surrounded by “?“ characters to the template name (?variable_name?). In this example, the “dw?DayOfWeek?” template contains the “DayOfWeek” template variable.
Add the expansion code for the “dw?DayOfWeek?” template. Use the Get(variable_name) text command to insert the variable value into the template expansion.
Click Apply and OK to save this template and close the options page.
Expand the “dw?DayOfWeek?” template. Type one of the following symbols in the code editor: “dw1”, “dw2”, “dw3”, etc. Then press Space or Tab depending on your settings. CodeRush expands “dw1” as DayOfWeek.Sunday, “dw2” as DayOfWeek.Monday, etc.
The following screencast shows the “dw1” expansion: