Alternative Template Expansions
- 2 minutes to read
This topic shows you how to enable Alternative Expansions for a template. You will specify several template expansions that will be used in different Contexts. The example starts with the vi?Type? template described in the Template Name Variables topic.
- Fill the Context field in the original template. It should expand when the clipboard does not contain an identifier. To check this, use the logically-inverted [HasIdentifier] Context Provider from the Editor | Clipboard group.
Click the + button on the Template Expansion toolbar to add an Alternative Expansion.
Specify the context for this expansion. For instance, use the following context: “[Hasldentifier] && ![DeclaresLocal(«?Paste»)]“. This expression checks whether or not the clipboard holds a valid identifier and this identifier is free in the current scope. If so, you can use this identifier to suggest the name for the new variable.
Note
To check if the identifier is declared locally, use the [DeclaresLocal(localName)] context provider from the Editor | Code group. You can use any String Provider as the Context Provider’s parameter.
Add one more expansion to cover the case when the identifier in the clipboard is already declared. In this case, you can assign a new variable value without declaration.
The template with three Alternative Expansions is now fully defined, and you can save and test it.