Embeddable engine for the T4 templating language, a general-purpose way to generate text or code files using C#
$ dotnet add package Mono.TextTemplatingNOTE: To use a template at runtime in your app, you do not need to host the engine. It is generally preferable to use
dotnet-t4to generate a runtime template class and compile that into your app, as this has substantially less overhead than hosting the engine.
Mono.TextTemplating is an open-source reimplementation of the Visual Studio T4 text templating engine, and supports C# 10 and .NET 6. This package is the engine package, which can be used to host the T4 engine in your own app.
By default the engine uses the C# compiler from the .NET SDK, but the Mono.TextTemplating.Roslyn package can be used to bundle a copy of the Roslyn C# compiler and host it in-process. This may improve template compilation performance when compiling multiple templates, and guarantees a specific version of the compiler.
This will read a template from templateFile, compile and process it, and write the output to outputFile:
var generator = new TemplateGenerator ();
bool success = await generator.ProcessTemplateAsync (templateFilename, outputFilename);
This does the same thing as a series of lower-level steps, allowing it to provide additional compiler arguments by modifying the TemplateSettings:
string templateContent = File.ReadAllText (templateFilename);
var generator = new TemplateGenerator ();
ParsedTemplate parsed = generator.ParseTemplate (templateFilename, templateContent);
TemplateSettings settings = TemplatingEngine.GetSettings (generator, parsed);
settings.CompilerOptions = "-nullable:enable";
(string generatedFilename, string generatedContent) = await generator.ProcessTemplateAsync (
parsed, inputFilename, inputContent, outputFilename, settings
);
File.WriteAllText (generatedFilename, generatedContent);
In most cases, you need only use or subclass TemplateGenerator:
ITextTemplatingEngineHost and ITextTemplatingSessionHost with a default implementation that can be overridden if needed.TemplateEngine instance and provides simplified ProcessTemplateAsync() and PreprocessTemplateAsync() methods.Mono.TextTemplating has session, host and directive processor interfaces and classes in the Microsoft.VisualStudio.TextTemplating namespace that are near-identical to the original Visual Studio T4 implementation. This allows older T4 templates and directive processors to work with Mono.TextTemplating with few (if any) changes.
The Microsoft.VisualStudio.TextTemplating.(ITextTemplatingEngine,Engine) hosting API is supported but deprecated.
For advanced use, some lower level classes and methods are accessible:
TemplatingEngine: generates C# classes from T4 templates and compiles them into assembliesTemplateGenerator.ParseTemplate(): uses a Tokenizer to parse a template string into a ParsedTemplateTokenizer: tokenizes an T4 input streamParsedTemplate: provides direct access to the segments and directives of a parsed templateTemplatingEngine.GetSettings(): uses the directives in a ParsedTemplate to initialize a TemplateSettingsTemplateSettings: settings that control code generation and compilation.CompiledTemplate: a template that has been compiled but not executedThe Mono.TextTemplating engine contains many improvements over the original Visual Studio T4 implementation, including:
AssemblyLoadContext, which allows the generated assemblies to be garbage collected (where supported)<#@ parameter name="Foo" type="int" #>