Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 47 additions & 10 deletions docs/core/tutorials/cli-templates-create-item-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,58 @@ Open the _template.json_ with your favorite text editor and paste in the followi

```json
{
"$schema": "http://json.schemastore.org/template",
"author": "Me",
"classifications": [ "Common", "Code" ],
"identity": "ExampleTemplate.StringExtensions",
"name": "Example templates: string extensions",
"shortName": "stringext",
"tags": {
"language": "C#",
"type": "item"
"$schema": "http://json.schemastore.org/template",
"author": "Me",
"classifications": [ "Common", "Code" ],
"identity": "ExampleTemplate.StringExtensions",
"name": "Example templates: string extensions",
"shortName": "stringext",
"tags": {
"language": "C#",
"type": "item"
},
"symbols": {
"ClassName":{
"type": "parameter",
"description": "The name of the code file and class.",
"datatype": "text",
"replaces": "StringExtensions",
"fileRename": "StringExtensions",
"defaultValue": "StringExtensions"
}
}
}
}
```

This config file contains all the settings for your template. You can see the basic settings, such as `name` and `shortName`, but there's also a `tags/type` value that is set to `item`. This categorizes your template as an item template. There's no restriction on the type of template you create. The `item` and `project` values are common names that .NET recommends so that users can easily filter the type of template they're searching for.

The `symbols` part of this JSON object is used to define the parameters that can be used in the template. In this case, there is one parameter defined, `ClassName`, which has several properties. The `type` property specifies that this is a parameter, the `description` property provides a description of the parameter, the `datatype` property specifies that the value of this parameter should be text, the `replaces` property specifies the text that should be replaced by the value of this parameter, the `fileRename` property specifies that the file should be renamed using the value of this parameter, and the `defaultValue` property specifies the default value for this parameter. This means that when this template is used, the user can provide a value for the `ClassName` parameter, and this value will be used to replace all occurrences of `StringExtensions` in the template and to rename the file. If no value is provided, then `StringExtensions` will be used as the default value. To see what parameters are avalible for the item template the user can run `dotnet new stringext -?` to see the avalible parameters.

```console
dotnet new stringext -?
Example templates: string extensions (C#)
Author: Me

Usage:
dotnet new stringext [options] [template options]

Options:
-n, --name <name> The name for the output being created. If no name is specified, the name of the output directory is used.
-o, --output <output> Location to place the generated output.
--dry-run Displays a summary of what would happen if the given command line were run if it would result in a template creation.
--force Forces content to be generated even if it would change existing files.
--no-update-check Disables checking for the template package updates when instantiating a template.
--project <project> The project that should be used for context evaluation.
-lang, --language <C#> Specifies the template language to instantiate.
--type <item> Specifies the template type to instantiate.

Template options:
-C, --ClassName <ClassName> The name of the code file and class.
Type: text
Default: StringExtensions

```

The `classifications` item represents the **tags** column you see when you run `dotnet new` and get a list of templates. Users can also search based on classification tags. Don't confuse the `tags` property in the \*.json file with the `classifications` tags list. They're two different things unfortunately named similarly. The full schema for the *template.json* file is found at the [JSON Schema Store](http://json.schemastore.org/template). For more information about the *template.json* file, see the [dotnet templating wiki](https://github.com/dotnet/templating/wiki).

Now that you have a valid _.template.config/template.json_ file, your template is ready to be installed. In your terminal, navigate to the _extensions_ folder and run the following command to install the template located at the current folder:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Open the _template.json_ with your favorite text editor and paste in the followi
"identity": "ExampleTemplate.AsyncProject",
"name": "Example templates: async project",
"shortName": "consoleasync",
"sourceName":"ExampleTemplate.AsyncProject",
"tags": {
"language": "C#",
"type": "project"
Expand All @@ -121,6 +122,8 @@ Open the _template.json_ with your favorite text editor and paste in the followi

This config file contains all of the settings for your template. You can see the basic settings such as `name` and `shortName` but also there's a `tags/type` value that's set to `project`. This designates your template as a project template. There's no restriction on the type of template you create. The `item` and `project` values are common names that .NET recommends so that users can easily filter the type of template they're searching for.

The `sourceName` item is what is replaced when the user uses the template. The value of `sourceName` in the config file, is searched for in any file name and file content. In this case test will replace `consoleasync` in the name of the .csproj file. By default, this value uses the name of the current folder. When the `-n` or `--name` parameter is passed with the `dotnet new` command, the value provided is used instead of the current folder.

The `classifications` item represents the **tags** column you see when you run `dotnet new` and get a list of templates. Users can also search based on classification tags. Don't confuse the `tags` property in the json file with the `classifications` tags list. They're two different things unfortunately named similarly. The full schema for the *template.json* file is found at the [JSON Schema Store](http://json.schemastore.org/template). For more information about the *template.json* file, see the [dotnet templating wiki](https://github.com/dotnet/templating/wiki).

Now that you have a valid _.template.config/template.json_ file, your template is ready to be installed. Before you install the template, make sure that you delete any extra folders and files you don't want included in your template, like the _bin_ or _obj_ folders. In your terminal, navigate to the _consoleasync_ folder and run `dotnet new install .\` to install the template located at the current folder. If you're using a Linux or macOS operating system, use a forward slash: `dotnet new install ./`.
Expand Down