HoofMark · Open Source
cstemplate lets you write code generators as ordinary C# files, with full IntelliSense, go-to-definition, and compile-time checking — no string templates, no external DSL, no build pipeline required.
Concept
Write a class that implements ITemplate, run it with the VS Code
extension or CLI, and generated files appear on disk. No magic, no configuration
language to learn.
Templates compile as part of your project. C# Dev Kit gives you completions, refactoring, and error squiggles as you type.
Reference any NuGet package in your templates — including packages with native dependencies like Microsoft.Data.SqlClient.
Read XML, JSON, or any file format with context.ReadFile(). Relative paths resolve from the template directory automatically.
Each template runs in its own AssemblyLoadContext. No conflicts between templates or between template and host dependencies.
Right-click any .template.cs file to run it. Compile errors appear as inline diagnostics. Output panel shows what was generated.
The cstemplate CLI works standalone — integrate with any build pipeline, CI system, or editor.
Quickstart
Three steps from install to generated code.
Install the global dotnet tool, then install the VS Code extension from the marketplace.
$ dotnet tool install --global HoofMark.CSharpTemplating.Cli
<PackageReference Include="HoofMark.CSharpTemplating.Abstractions"
Version="1.0.0" />
Create a .template.cs file anywhere in your project:
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")
));
}
}
Right-click the file in VS Code and choose Run Template, or run from the terminal:
$ cstemplate run OrderServiceTemplate.template.cs
Configuration
Place a cstemplate.config.json file in your project folder (or any
parent folder) to control output location, NuGet packages, and local references.
{
"outputRoot": "../generated",
"project": "./MyProject.csproj",
"nugetPackages": [
"Humanizer.Core",
"Microsoft.Data.SqlClient"
],
"references": [
"path/to/LocalAssembly.dll"
]
}
| Field | Description |
|---|---|
outputRoot |
Output directory for generated files, relative to this config file. |
project |
Path to the .csproj file. Required when using nugetPackages. |
nugetPackages |
NuGet package IDs to make available in templates. Versions and transitive dependencies are resolved from dotnet restore. |
references |
Paths to local .dll assemblies, relative to this config file. |
VS Code Settings
| Setting | Default | Description |
|---|---|---|
cstemplate.cliPath | cstemplate | Path to the cstemplate CLI executable. |
cstemplate.checkOnSave | true | Compile-check templates on save, showing errors as diagnostics. |
cstemplate.showOutputOnRun | true | Show output panel when a template is run. |
cstemplate.outputRoot | empty | Default output root. Falls back to generated/ next to each template. |
cstemplate.templateFilePattern | **/*.template.cs | Glob pattern used to identify template files. |
Documentation
Full API reference, CLI documentation, and configuration guides.
Complete reference for ITemplate, ITemplateContext,
IOutputWriter, and all public types in the
CsTemplate.Abstractions and CsTemplate.Core packages.
Commands, arguments, options, exit codes, and JSON output format for the
cstemplate command-line tool.
Full schema reference for cstemplate.config.json — output roots,
NuGet package resolution, and local assembly references.