Hack 57. Insert Commonly Used Code Automaticallytemplates and code completion to enable you to write code faster. Throughout this book, we have talked about different ways to handle code snippets, including storing them in the toolbox or using other add-ins to handle your code snippets. The Code<Template>.NET add-in allows you to create and manage code templates, which are code snippets that contain variables. Instead of just having a piece of text you can drag to your document, you will be able to specify variables that you will be prompted for. What differentiates this add-in from other code snippet management add-ins is the ability to define variables as well as the ability to automatically insert your code template using a predefined keyword and shortcut key.
Code<Template>.NET add-in from http://www.codeproject.com/useritems/CodeTemplateNET.asp. The installation is relatively painless, and the next time you start Visual Studio you will see the CodeTemplate toolbar, which is shown in Figure 6-18. Figure 6-18. CodeTemplate toolbar![]() let's add a new code template to the tool. Click Open <codetmpl.cs> on the CodeTemplate menu. The codetmpl.cs file contains all of the code templates that this tool manages. It is to this file that you will need to add your own code templates. As you can see, the format of this file is a little confusinghere is a look at one of the standard code templates: #{Subsection Header|subhEach snippet starts with #{ and then thename of the snippet, in this case Subsection Header. After the name of the template is a | character followed by a keyword, which will be used to autoinsert the code into your document. What follows next is the actual code that will be inserted into the document. This can also include variables. In this case you see <%?Subsection name%> in the text. When the template is inserted, the add-in will prompt you with a dialog like the one shown in Figure 6-19. Figure 6-19. CodeTemplate variable dialog![]() insertion. The last part of the code template is the ending }#. So let's add a new code snippet to this file:First you need to open the template and create a name and shortcut key for it: #{SQLConnection|sqlcNext you need to specify the actual text that will be inserted,including any variables: SqlConnection sqlConn = new SqlConnection(Then you need to close the template: }#Next you simply need to save the file, and the menu will be updated.Now that the code template is added to the file, it can be invoked in two different ways. The first is to select the code template from the menu; the second is to type sqlc into the document and then press Ctrl-Space (You may want to reassign this shortcut key, as it tends to interfere with the normal Ctrl-Space shortcut.) After performing either of these functions, you will be presented with the variable dialog shown in Figure 6-20. Figure 6-20. SQLConnection variable dialog![]() press the OK button, the code shown here will be inserted into the document. SQLConnection sqlConn = new SQLConnection(As you can see, this add-in can help quickly insert code snippets, including code snippets that include variables.This add-in also includes a number of default keywords that can be used in your templates; these are used by enclosing the keyword between <% and %> characters. (Notice that, unlike ConfigKey in the preceding example, there is no question mark; those are used only for variables that will be specified by the user.) You can insert any of the keywords shown in Table 6-4 in the template, and the keyword will be replaced with the value specified in the table.
interesting since it allows you to nest templates within each other. Here is an example of how to use this keyword: <%template:sqlc%>This would insert the output from the template defined earlier into another template. You will still be prompted for any variables when the parent template is executed.The Code<Template>.NET add-in is a great way to decrease the amount of time you spend typing the same code over and over again. |



