Table of Contents

Quickstart

Prerequisites

  • .NET 10 SDK or later
  • VS Code with the C# Dev Kit extension

1. Install the CLI tool

dotnet tool install --global HoofMark.CSharpTemplating.Cli

2. Install the VS Code extension

Install cstemplate from the VS Code Marketplace.

3. Add the abstractions package

In your project's .csproj, add a reference to HoofMark.CSharpTemplating.Abstractions:

<ItemGroup>
  <PackageReference Include="HoofMark.CSharpTemplating.Abstractions" Version="0.1.0" />
</ItemGroup>

4. Write a template

Create a .template.cs file anywhere in your project. Each template is a class that implements ITemplate with a static void Run(ITemplateContext) method. Give each template a unique class name and namespace:

using HoofMark.CSharpTemplating.Abstractions;

namespace MyProject.Templates;

public class OrderServiceTemplate : ITemplate
{
    public static void Run(ITemplateContext context)
    {
        var ns = context.Config.Get("Namespace");

        context.WriteFile("OrderService.cs", w => w
            .WriteLine($"namespace {ns};")
            .Block("public class OrderService", body => body
                .WriteLine("// Generated by cstemplate")
            ));
    }
}

5. Add a config file

Place a sibling .json file with the same base name to supply config values:

{
  "Namespace": "MyApp.Generated"
}

So for OrderServiceTemplate.template.cs, create OrderServiceTemplate.json.

6. Run the template

From VS Code: right-click OrderServiceTemplate.template.cs in the Explorer and choose Run Template.

From the terminal:

cstemplate run src/templates/OrderServiceTemplate.template.cs

The generated OrderService.cs will appear in a generated/ folder next to the template, or in the outputRoot specified in cstemplate.config.json.

Next steps