Template variables
The body of a code template may contain:
Plain text that represents source code constructs and comments. It is inserted as is when you apply the template.
Template variables that are replaced dynamically when you apply the template. A template variable has the following format:
$PARAM_NAME$
- wherePARAM_NAME
is an arbitrary identifier.
Predefined variables
The following template variables are predefined in JetBrains Rider:
$END$
— the caret position after the template is applied.$SELECTION$
— the text selected by the user before invoking the template. This variable is only used in surround templates.$SELSTART$
— the starting position of the text block that will be selected after the template is applied.$SELEND$
— the ending position of the text block that will be selected after the template is applied.
User-defined variables
All other variables can be defined with arbitrary identifiers when you create or edit a template. Normally you will need to supply your variables with macros for calculating their values when the template is applied.
Also, you can make each variable editable so that the user can change its value when the template is applied.
Editable variables
If a code template has no editable variables, it is applied instantly without user interactions; all non-editable variables, if any, are calculated automatically with macros (for example, by inserting a fixed value, the current namespace, type name, or even the clipboard content).
After the template code is inserted into your source code, you resume typing at the position specified by template developer with the $END$
or $SELEND$
variables, but if you need to change some parts of the inserted code, you will be able to do so only by going back and editing it.
In contrast, editable variables will expose the fields that may need to be changed and, often, reasonable choices will be suggested for them by the underlying macros. As you are getting acquainted with JetBrains Rider, you will see that many of its predefined templates have editable variables. You can also study Create and Edit Templates section to learn how to create and edit custom templates with editable variables.
For example, a well-designed template for iteration over all elements of a container will use editable variables to allow you choosing:
A suitable name for the loop index variable (for example,
i
orj
).A list of all variables of the specified type in the current scope (for example, Arrays or Lists) as an expression for the iterated container.
A suitable name for the assigned variable that will hold the current container element during the iteration.
The type of elements in the iterated container.
When a template with editable variables is applied, JetBrains Rider deploys a Hot Spot Session in the editor. This session lets you specify or choose values for all variables, step by step. You can either replace the variables with your input or accept one of the suggested values.
Examples of applying templates with editable variables are given in the Creating Source Code With Live Templates and Surrounding Code Fragments With Templates sections.