JetBrains Rider 2022.2 Help

Declare template variables

Code templates can adjust to the surrounding context or even act interactively when you apply them. All this is done with template variables and template macros.

You can define and edit template variables on the template settings pages, when a specific template is selected. As soon as you type a variable (prefixed and suffixed with $), it becomes available in the Edit variables dialog . You can then configure its behavior by clicking Change macro:

JetBrains Rider: Template editor

You can use any variable in a template more than once; their values will be synchronized when the template applies.

Declare a template variable

  1. Create a new template or open an existing template for editing.

  2. Enter a variable as $variable_name$ in the text area of the template editor.

  3. To define a macro that calculates variable value, find the variable in the Edit variables dialog and click Change macro.

  4. In the Change macro dialog that appears, select a macro from the list and click OK. If the selected macro has a macro-parameter, is displayed in bold in the Choose Macro dialog (for example, format or type). In this case, after you choose a macro, a text field or a selector appears under the template variable. You can specify the macro-parameter there.

  5. If you want the variable to be editable, the Editable checkbox next to it should be enabled (which it is by default). Depending on the number of variable occurrences within the template, do one of the following:

    • If the variable is used in the template only once, make sure that the checkbox is selected.

    • If the variable is used in the template more than once, a combo box appears, where you can select a sequential number of the editable occurrence. This number specifies to which occurrence of the variable the input focus will be set when the template is deployed with a Hot spot session. You may also notice that the numbers are prefixed with 'M' or other letters. These letters define in which file of a multi-file template the input focus should be set for this variable. 'M' stands for the main file and appears in all templates.

  6. Optionally, you can select the newly declared variable in the Parameters area and use up and down arrows to change the order of variable. The position in the list of variables defines the order, in which the variables are switched during the hot spot session when the template is applied. It affects only editable variables.

  7. Click Save in the Settings dialog to apply the modifications and let JetBrains Rider choose where to save them, or save the modifications to a specific settings layer using the Save To list. For more information, see layer-based settings.

Last modified: 19 August 2021