Telerik blogs

Template Basics

JustCode templates are much like Visual Studio code snippets. They give you a chance to seamlessly generate a piece of code following a predefined structure. In the general case you just have to pick up the real content, i.e. the stuff that actually defines the logic of the code, from a set of possible choices that JustCode computes for you. All the boilerplate code is automatically generated for you.

For example, here is how the C# built-in foreach code template  normally works out:

Step 1 – Expand the template


Step 2 – Choose a collection variable to iterate over

 

Notice how the type of e automatically changes from string to int

 

Step 3 - Choose or type in  a name for the variable foreach loop declares

 

We type in a more descriptive name and get:

 

Step 4 – Accept the template

That is what you get as a result.

 


Triggering and Setting Up JustCode Templates

The templates that make sense at any given position in the code could be expanded from the context menu that comes out when you hit Alt+End.

JustCode comes with a set of predefined templates for the programming languages it supports. The real power of the code templates, though, stems from the ability to define your own code templates by going to JustCode -> Options -> Code Templates. Have a look at the next section to find out how.

 

Defining Your Own Template

 

General

Any text you put in the code template editor is reproduced in the Visual Studio code editor when the template gets expanded unless it belongs to any of the following categories – template variable, template function or special character. Anything that belongs to these categories gets evaluated by JustCode when the template is expanded and it gets replaced with the output of that evaluation.

 

Template Variables

The syntax for defining a template variable is:

 

$number[=function]$

 

Where number is any natural number and function is any of the template functions in the Template Functions section.

 

So, the template variables are designated by non-repeating natural numbers, i.e. 1,2,3…. Apart from serving as unique identifiers for the template variables these numbers also specify the order in which the user gets to pick values for the template variables. The lower numbered variables are visited before the higher numbered ones.

 

There is a very important consequence of the order in which the user gets to choose values for the template variables. Namely, $2$ can be a function of $1$ but $1$ cannot be a function of $2$ since the value of $2$ is not specified by the user at the time $1$ gets to be specified.

 

Template Syntax Example:

                        Here we have a basic template for an if statement

 

if ($1=SELECT_VARIABLE(bool)$)

{

|

}             

                                

                                Here we are defining a template variable that serves as a condition of the if statement and we are designating it with 1. When the template gets expanded that variable will get replaced by the return value of the function SELECT_VARIABLE if it returns a single item (i.e. there is only one boolean member in scope). If it returns more than one item (i.e. there is more than one boolean member in scope) the user will be given the chance to choose which one of the list of items returned will replace $1$. We can further develop our template by referring  to that variable again.

 

if ($1=SELECT_VARIABLE(bool)$)

{

                                $1$  = false;  //this will now output the value of $1$ variable inside the if block, and set its value to false

}

 

Expanded Template: 

bool test = true; //declared in my code

              //below is what my new if template generated for us.

              if ( test )

              {

                 test = false;

              }

 

EXAMPLE:

A foreach template is a good example of template variable specification order.

foreach (  $3=GET_ELEMENT_TYPE($1$)$    $2=SUGGEST_ELEMENT_NAME($1$)$     in      $1=SELECT_VARIABLE([],System.Collections.ArrayList)$  )

{

|

}

The user gets to choose the value of $1$ first when the template is expanded. That way the choices the user can make for the values of $2$ and $3$ get evaluated based on the chosen value of $1$ and the user gets the respective values to choose from once he moves forward to specifying values for these variables.

 

Template Functions

SELECT –

This allows you to give the template user a list of predefined constant values when the template is run.   It works much like a combobox.    

               

                EX:

                                Template:

                                                $6=SELECT(choice one,choice two,choice three)$

                                               

SELECT_VARIABLE -

Tries to find valid members of a given type(s) that are in scope.  It will look for fields, and properties of the specified type.  It will also look for methods that return the specified types, and require no parameters. If you do not pass in types for it to search for, then it will get all variables in scope.

Arguments: A comma separated list of variable types to search for.

Template Syntax Example:

 

SELECT_VARIABLE(System.Collections.Generic.List)

 

This will find all members of type “List”  available in the current context. 

 

You can also specify multiple valid types by separating them with a comma

SELECT_VARIABLE(int,uint).


SUGGEST_NAME - 

Return  suggestions for identifier name in accordance with the syntax rules of the programming language in use.

 

Template Syntax Example:

bool  $1=SUGGEST_NAME$ = false;

do {

…….

……

}

while($1$)

  

SUGGEST_ELEMENT_NAME- 

This will try to come up with a good identifier name for an identifier that is a placeholder for the elements of a collection being iterated through (e.g. as in C# foreach syntax). The function argument should be evaluated to a collection type variable identifier or a list of such identifiers.  

Arguments:  Could be evaluated to a collection or a list of collections. At present, of all template functions only the SELECT_VARIABLE template function returns such a list.

 

Template Syntax Example:

 

foreach (  $3=GET_ELEMENT_TYPE($1$)$    $2=SUGGEST_ELEMENT_NAME($1$)$     in      $1=SELECT_VARIABLE([],System.Collections.Generic.IList)$  )

{

|

}

 

$1$ will hold a list of all the arrays and IList implementers in scope.

 

Code Example

                Member In Scope:

List<string> myItems = new List<string>();

 

                Result:

                foreach (string myItem in myItems)

                {

                

                 }

Code Example:

                Member In Scope:

                                List<string> myList = new List<string>();

 

                Result: 

 foreach (string e in myList)

  {

                

  }

 

SUGGEST_INDEX_NAME-

Tries to suggest a name for an index variable in a loop.

 

SUGGEST_EXCEPTION_NAME-

Tries to suggest a name for an exception.

 

GET_ELEMENT_TYPE-

              Returns the type of the identifier given as an argument. 

 

                           Arguments:  Could be evaluated to an identifier.

 

               Template Syntax Example:

    $1=SELECT_VARIABLE(System.Collections.Generic.List,System.Collections.Generic.IList)$

$2=GET_ELEMENT_TYPE($1$)$

This would output the type for the elements contained by an IList<> found in the current scope.

 

STATEMENT_SELECTION-

A placeholder for any code the user might have highlighted prior to starting the    template. You could see an example of this in all the “Surround with” templates 


Special Characters in Template Definition Syntax

 

|   marks the desired position  of the cursor right after the user has committed all the changes on the template


$  marks the beginning/end of a template variable declaration


\   escape character. Place it before | or $  or \ if they are not meant to be part of the template syntax, i.e. you want them literally reproduced when the template gets expanded. 


About the Author

Chris Eargle

is a Microsoft C# MVP with over a decade of experience designing and developing enterprise applications, and he runs the local .NET User Group: the Columbia Enterprise Developers Guild. He is a frequent guest of conferences and community events promoting best practices and new technologies. Chris is a native Carolinian; his family settled the Dutch Form region of South Carolina in 1752. He currently resides in Columbia with his wife, Binyue, his dog, Laika, and his three cats: Meeko, Tigger, and Sookie. Amazingly, they all get along... except for Meeko, who is by no means meek.

Comments

Comments are disabled in preview mode.