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 JetBrains Rider: Template editor](https://resources.jetbrains.com.cn/help/img/rider/2024.2/template_editor.png)
You can use any variable in a template more than once; their values will be synchronized when the template applies.
Declare a template variable
Create a new template or open an existing template for editing.
Enter a variable as
$variable_name$
in the text area of the template editor.To define a macro that calculates variable value, find the variable in the Edit variables dialog and click Change macro.
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 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.
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.
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.
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 by choosing this layer from the Save selector. For more information, see layer-based settings.