pauldeen79.TemplateFramework.Core
0.3.0
See the version list below for details.
dotnet add package pauldeen79.TemplateFramework.Core --version 0.3.0
NuGet\Install-Package pauldeen79.TemplateFramework.Core -Version 0.3.0
<PackageReference Include="pauldeen79.TemplateFramework.Core" Version="0.3.0" />
paket add pauldeen79.TemplateFramework.Core --version 0.3.0
#r "nuget: pauldeen79.TemplateFramework.Core, 0.3.0"
// Install pauldeen79.TemplateFramework.Core as a Cake Addin #addin nuget:?package=pauldeen79.TemplateFramework.Core&version=0.3.0 // Install pauldeen79.TemplateFramework.Core as a Cake Tool #tool nuget:?package=pauldeen79.TemplateFramework.Core&version=0.3.0
TemplateFramework
Template and code generation framework for C#
If you want to create templates in any .NET language (C#, VB.Net, F#) and run them using a dotnet global tool, this framework is for you!
We currently target .NET 7.0, but the code can easily be ported back to older .NET versions.
Difference between a template and code generation provider
A code generation provider is a class that provides a template instance, along with optional model and additional parameters. This is typically the level you want to use, if you want to scaffold multiple files using a single command.
If you want to use the template abstraction level, then you have to make sure the template class has a public parameterless constructor.
Features
- Runs templates or code generation providers from .NET assemblies from command line
- Supports generating single or multiple files from one template
- Supports custom hosting of the template engine or code generation engine, if you want to
- Writes output to either console, clipboard or file system
- Supports child templates, if you want to split your template into multiple logical templates
- Battle tested
- Extensible using dependency injection (write new implementations, and register them in your DI container)
Packages
- TemplateFramework.Abstractions: Interfaces for templates, code generation providers and generation environments
- TemplateFramework.Core: Template engine and code generation engine, and all needed implementations for abstractions
- TemplateFramework.Console: Dotnet tool that can be launched from command line (using tf command)
- TemplateFramework.TemplateProviders.ChildTemplateProvider: Adds support for child templates
- TemplateFramework.TemplateProviders.CompiledTemplateProvider: Adds support for compiled templates
How to create a template
You have to write a class in a .NET 7.0 project (class library project is good enough), and compile this project. Then you can either use the command line tool 'tf' (Template Framework) or write your own host and reference the Core and TemplateProviders.CompiledTemplateProvider packages.
There are multiple types of templates supported out of the box:
- StringBuilder template, which appends to a single output using a StringBuilder which is passed as an argument
- Text Transform template, which has one method with a return type of string, that is called to run the template
- Multiple Content Builder template, which allows to create more output files
- POCO template. If the class is not of a supported type, then the ToString method will be called on the template instance
To create a StringBuilder template, implement this interface from the TemplateFramework.Abstractions package:
public interface IStringBuilderTemplate
{
void Render(StringBuilder builder);
}
To create a Text Transform template, implement this interface from the TemplateFramework.Abstractions package:
public interface ITextTransformTemplate
{
string TransformText();
}
To create a Multiple Content Builder template, implement this interface from the TemplateFramework.Abstractions package:
public interface IMultipleContentBuilderTemplate
{
void Render(IMultipleContentBuilder builder);
}
How to add package references to your template assembly, when using the (Console) command tool
The template assembly is loaded by the command tool. If you want to add external references to your template assembly, then you have to follow some additional steps.
First option is to write a custom host. Add the references to the Core and TemplateProviders.CompiledTemplateProvider packages.
Second option is to use the plug-in architecture as documented by Microsoft. (https://learn.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support) Note that this option hasn't been tested yet.
Third option is to add the following property to your template assembly:
<PropertyGroup>
...
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
...
</PropertyGroup>
How to call child templates from your main template
If you want to render child templates from your main (root) template, then you have to implement these interfaces from the TemplateFramework.Abstractions package: ITemplateContextContainer and ITemplateEngineContainer.
public interface ITemplateContextContainer
{
ITemplateContext Context { get; set; }
}
public interface ITemplateEngineContainer
{
ITemplateEngine Engine { get; set; }
}
Then, in your template, call the Render method on the TemplateEngine instance. (TemplateEngine property) As context, create a child context using the CreateChildContext method on the TemplateContext instance. (Context property)
There is also an integration test in the TemplateFramework.TemplateProviders.ChildTemplateProvider test project to demonstrate this.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- CommunityToolkit.Diagnostics (>= 8.2.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- pauldeen79.TemplateFramework.Abstractions (>= 0.3.0)
NuGet packages (7)
Showing the top 5 NuGet packages that depend on pauldeen79.TemplateFramework.Core:
Package | Downloads |
---|---|
pauldeen79.TemplateFramework.TemplateProviders.ChildTemplateProvider
Framework for template processing and code generation |
|
pauldeen79.TemplateFramework.Core.CodeGeneration
Framework for template processing and code generation |
|
pauldeen79.ClassFramework.TemplateFramework
Package Description |
|
pauldeen79.TemplateFramework.TemplateProviders.CompiledTemplateProvider
Framework for template processing and code generation |
|
pauldeen79.TemplateFramework.TemplateProviders.StringTemplateProvider
Framework for template processing and code generation |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.1.1 | 20 | 11/24/2024 |
1.1.0 | 167 | 11/17/2024 |
1.0.3 | 375 | 10/30/2024 |
1.0.2 | 175 | 10/18/2024 |
1.0.1 | 266 | 9/21/2024 |
1.0.0 | 191 | 9/16/2024 |
0.11.1 | 991 | 5/18/2024 |
0.11.0 | 197 | 5/17/2024 |
0.10.2 | 328 | 5/9/2024 |
0.10.1 | 275 | 5/3/2024 |
0.10.0 | 359 | 4/19/2024 |
0.9.3 | 479 | 4/3/2024 |
0.9.2 | 448 | 3/22/2024 |
0.9.1 | 299 | 3/15/2024 |
0.9.0 | 782 | 3/3/2024 |
0.8.7 | 1,616 | 1/5/2024 |
0.8.6 | 967 | 12/7/2023 |
0.8.5 | 360 | 11/29/2023 |
0.8.4 | 201 | 11/27/2023 |
0.8.3 | 157 | 11/27/2023 |
0.8.1 | 241 | 11/24/2023 |
0.8.0 | 239 | 11/19/2023 |
0.7.4 | 221 | 9/13/2023 |
0.7.3 | 182 | 9/10/2023 |
0.7.1 | 201 | 9/9/2023 |
0.7.0 | 197 | 9/8/2023 |
0.6.0 | 214 | 9/7/2023 |
0.5.1 | 183 | 9/4/2023 |
0.5.0 | 202 | 9/4/2023 |
0.4.0 | 176 | 8/23/2023 |
0.3.2 | 203 | 8/14/2023 |
0.3.1 | 173 | 8/13/2023 |
0.3.0 | 154 | 8/13/2023 |
0.2.0 | 187 | 8/11/2023 |
0.1.0 | 187 | 7/23/2023 |