Templates Galore: File Templates

Continuing with Templates, in this post we’re going to take a look at File Templates. Whereas Live Templates allow us to generate interactive code and insert this at the current cursor position, File Templates allow us to do the same but directing the output to a file. Additionally, as we’ll see, they are very useful for creating both standard and custom files efficiently.

If we open up the Templates Explorer via ReSharper | Live Templates, we can see three tabs: Live Templates, Surround Templates and File Templates.

SNAGHTML2cd01427

Similar to Live Templates, Files are organized into different categories, corresponding to the type and language of a project. By default the predefined ones include templates for Web Applications (ASP.NET) and generic applications (Console, WinForm, Libraries, etc.). There are also User defined ones as well as Shared Solution and Personal Solution, again similar to Live Templates.

On the right-hand column we have a repeated set of templates. What these allow us to do is customize the usage of templates. However, before seeing that and delving into creating new templates, let’s see how we can use the existing ones.

Using Templates in Web Applications

1. Create a new Web Application (can be MVC or WebForms) to work with.

2. There are multiple ways to create new file templates with ReSharper (right-click Solution Explorer |Add | New From Template or via the ReSharper | Edit | New From Template menu). However, by far one of the easiest and most efficient ways is what we introduced in ReSharper 5: Create New File. You can invoke this from anywhere, be it the Solution Explorer or from a file in the editor, by pressing Ctrl+Alt+Ins

image 
[Pressing Ctrl+Alt+Ins in ASPX editor]

 

image 
[Pressing Ctrl+Alt+Ins in Solution Explorer]

Since this is a Web Application, the list of Templates to choose from include all those that appeared under ASP.NET Projects in the Template Explorer. In addition, since creating a Class, Interface, Struct or Enum are still valid in the context, it will display those too.

3. Select a new file, for instance Web form with master page and provide it with a name and hit OK (Enter key)

SNAGHTML2cee1c61

4. Based on the type of file, and in particular the template it contains, we will be prompted for additional information. In this case since we requested a Web Form with a MasterPage, ReSharper prompts for the MasterPage file

image

5. Select the corresponding master page and hit Enter.

We’re done. With very little effort, we’ve created a new Web Page and referenced the Master Page. Comparing that to the default way of creating Web Forms (or in fact any other item) in Visual Studio, we can quickly see how much more efficient it can be. However, the true power of this lies in it’s customization, not only in the template but also in what templates we can choose from.

Customizing the Files we care about

When invoking the Create New File, the list that appears is customizable, both in regard to the contents as well as the ordering. Imagine for instance that we usually work with MVC applications and Spark View Engine and are not really interested in WebForms. Having things like Web Control or Web Form with master page pop-up only add noise to the options. Fortunately we can eliminate these easily:

1. Open up the Templates Explorer editor via ReSharper | Live Templates and select File Templates

2. Select the element we don’t want to appear when creating a new File Template. In this case we want to eliminate Web Controls and Web form xxxx

SNAGHTML2d038f50

3. Click on the red cross (X)

4. Close the Templates Explorer

5. Invoke the Create New File menu by pressing Ctrl+Alt+Ins. It should now only display the remaining entries.

image

Apart from eliminating entries, we can also change the sort order by dragging the entries in the Templates Explorer (right-hand column, same place we selected to delete in step 2) up and down.

Creating new File Templates

Being able to generate templates fast, customize them and edit them wouldn’t be sufficient if we couldn’t also create new ones. Fortunately, we can. Let’s see how we can easily create simple DTO objects that use for instance the DataContract and DataMember attributes.

1. Create a new project. Any type will do.

2. Open Templates Explorer via ReSharper | Live Templates and click on File Templates

3. Select the User Templates node and right-click to create New Template

image

3. In the Template Editor, type DTO for the Description and NewDTO.cs for the file name

4. Enter the following in the Editor

SNAGHTML2d410809

We now need to define some macros on the placeholders (variables $nameSpace$, $className$, etc.).

Note: The first [DataMember] property we’ve added is of course optional and more for demonstration purposes.

5. Select the nameSpace placeholder in the right-hand column and for the value choose “Default namespace for current file

6. Select the className placeholder in the right-hand column and for the value choose “Current file name without extension

7. Uncheck the Editable Occurrence on previous two entries. This prevents ReSharper prompting us for values

8. Save changes and close the editor

Using the new File Template

We saw that File Templates can be invoked using Ctrl+Alt+Ins (Create New File). However if we do this now, our template will not appear. That is because we need to tell ReSharper we want it on this quick list. We can do this in two ways:

Method 1: Invoke the template using an alternative option, for instance ReSharper | Edit | New From Template and selecting More..

image

This will bring up a dialog box with other templates:

SNAGHTML2d4b70d9

where we can select our Template. At this point we can tick the CheckBox “Add to quicklist”, selecting the Category from the Dropdown below it.

Method 2: Alternatively, we can drag and drop the template from the Template Explorer to the Category we are interested in (for instance C#)

SNAGHTML2d50a8da

Either way we choose, from this point on, our template will now be accessible via Ctrl+Alt+Ins:

image

Summary

With File Templates we can easily create files that are used frequently, such as DTO’s, Unit Tests, Controllers or other common classes we create. In addition to providing an efficient way to create these items, we can also interact with them, just like we do with Live Templates to adapt them to our needs on each creation.

This entry was posted in How-To's, ReSharper Tips&Tricks and tagged , , . Bookmark the permalink.

14 Responses to Templates Galore: File Templates

  1. Matt Andreko says:

    Is there any way to setup a template that does multiple files, such as a Web Form in the aspx file, and then a CodeBehind file in the aspx.cs?

    Right now I have a File Template for the aspx, and I have to click “Add CodeBehind File” from the tooltip.

  2. Hadi Hariri says:

    @Matt,

    Currently there’s no easy way, although let me think on it a bit.

  3. Richard says:

    Another slight issue with adding a page or master page based on an existing master page: you have to manually create the content sections. The built-in Add dialog will prompt you for the master page first, and then create a file with an empty section for each in the master page.

  4. Not having the ability to generate the code-behind aspx.cs makes this a non-starter for me, I’ve had to go to visual studio templates.

  5. Hadi Hariri says:

    @Joel,

    I’m working on a blog post that hopefully will show how to do this. If I succeed, I’ll post it :)

  6. Is it possible to edit the shortcut keys assigned to items in the quicklist? By default “class” is “c” when in a class library, but “l” when in a web project, very annoying.

  7. Hadi Hariri says:

    @Andrew

    Currently there is no way.

  8. Chris says:

    Hadi,

    Any luck with being able to create multiple files with live templates?

  9. Hadi Hariri says:

    @Chris, @Andrew,

    Working on it. Will let you know ASAP.

  10. Nathan says:

    @Hadi,

    With the release of R# 6.0, is there functionality for multiple file templates?

  11. Hadi Hariri says:

    @Nathan,

    It didn’t make it in this build unfortunately. It is still on the list however.

  12. Eric says:

    @Hadi

    any updates on when we will be able to create multiple files from live templates ? maybe in 6.5?

  13. Hadi Hariri says:

    @Eric,

    I’m not seeing it scheduled unfortunately, but will check with team.

  14. Rebecca says:

    @Matt & @ Hadi,
    I found an easy way to do a ASP.Net Web form with a separate code behind file. Use the current ASP.NET template. Then in your solution explorer copy and paste the Web Form file it creates. Rename the file to match your Web Form and put a .cs extension after the name. The Solution Explorer will refresh. Now edit the Web Form and add the CodeBehind tag.

    Granted it is a longer solution than having Resharper automatically doing it but it is a workaround.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">