Template Parameters
This page is specific to:.NET Framework Version:2.03.54.0
Visual Studio
Template Parameters

All templates support parameter substitution to enable replacement of key parameters, such as class names and namespaces, when the template is instantiated. These parameters are replaced by the template wizard that runs in the background when a user clicks OK in the New Project or Add New Item dialog boxes.

Declaring and Enabling Template Parameters

Template parameters are declared in the format $parameter$. For example:

  • $safeprojectname$

  • $guid1$

  • $guid5$

To enable parameter substitution in templates

  1. In the .vstemplate file of the template, locate the ProjectItem element that corresponds to the item that you want to enable parameter replacement for.

  2. Set the ReplaceParameters attribute of the ProjectItem element to true.

  3. In the code file for the project item, include parameters where appropriate. For example, the following parameter specifies that the safe project name be used for the namespace in a file:

    namespace $safeprojectname$
    
Reserved Template Parameters

The following table lists the reserved template parameters that can be used by any template.

NoteNote:

Template parameters are case-sensitive.

Parameter

Description

clrversion

Current version of the common language runtime (CLR).

GUID [1-10]

A GUID used to replace the project GUID in a project file. You can specify up to 10 unique GUIDs (for example, guid1).

itemname

The name provided by the user in the Add New Item dialog box.

machinename

The current computer name (for example, Computer01).

projectname

The name provided by the user in the New Project dialog box.

registeredorganization

The registry key value from HKLM\Software\Microsoft\Windows NT\CurrentVersion\RegisteredOrganization.

rootnamespace

The root namespace of the current project. This parameter is used to replace the namespace in an item being added to a project.

safeitemname

The name provided by the user in the Add New Item dialog box, with all unsafe characters and spaces removed.

safeprojectname

The name provided by the user in the New Project dialog box, with all unsafe characters and spaces removed.

time

The current time in the format DD/MM/YYYY 00:00:00.

userdomain

The current user domain.

username

The current user name.

webnamespace

The name of the current Web site. This parameter is used in the Web form template to guarantee unique class names. If the Web site is at the root directory of the Web server, this template parameter resolves to the root directory of the Web Server.

year

The current year in the format YYYY.

Custom Template Parameters

In addition to the reserved template parameters that are automatically used during parameter replacement, you can specify your own template parameters and values. For more information, see How to: Pass Custom Parameters to Templates.

Example: Replacing Files Names

You can specify variable file names for project items by using a parameter with the TargetFileName attribute. For example, you could specify that the .exe file use the project name, specified by $projectname$, as the file name.

<TemplateContent>
    <ProjectItem
        ReplaceParameters="true"
        TargetFileName="$projectname$.exe">
            File1.exe
    </ProjectItem>
      ...
</TemplateContent>
Example: Using the Project Name for the Namespace Name

To use the project name for the namespace in a Visual C# class file, Class1.cs, use the following syntax:

#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace $safeprojectname$
{
    public class Class1
        {
            public Class1()
                {

                }
         }
}

In the .vstemplate file for the project template, include the following XML when you reference the file Class1.cs:

<TemplateContent>
    <ProjectItem ReplaceParameters="true">
        Class1.cs
    </ProjectItem>
    ...
</TemplateContent>
See Also

Other Resources

Community Content

$destinationdirectory$
Added by:Kirill Osenkov - MSFT

There is also the $destinationdirectory$ template parameter, which points to the location where the new project is saved.

$installpath$ - The path to the directory containing the project template used to generate the project.

See also: http://www.windowsdevcenter.com/pub/a/windows/2006/10/17/creating-visual-studio-project-templates.html?page=last

Some more parameters
Added by:Daniel Portella
you have the

$If$ (expression) ... $endif$

example

using System;
$if$ ($targetframeworkversion$ == 3.5)using System.Linq;
$endif$
using System.Text;
...

and

$targetframeworkversion$ that gives you the value of the framework version like 3.5

filename
Added by:bitbonk
is there also a parameter that gives me the exact filname (without the path) for the file that was just generated?
$contenttags$ $pagecontent$
Added by:Thomas Lee

When I look at MS item templates I see $pagecontent$ and $contenttags$... But when I try to use them they show up in the mark up (.aspx) as is. Others like $masterpage$ $codebesiderootname$ and $codebesideclassname$ all work. How do I get my template to grab content tags from it's master page like the MS templates?

[tfl - 05 09 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at

http://www.microsoft.com/communities/newsgroups/en-us/ . You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C &
SQL Server :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.sqlserver%2C &
.NET Framework :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
PowerShell : http://groups.google.com/group/microsoft.public.windows.powershell/topics?pli=1
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C &

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View