Templaty 1.0.1

dotnet add package Templaty --version 1.0.1
                    
NuGet\Install-Package Templaty -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Templaty" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Templaty" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Templaty" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Templaty --version 1.0.1
                    
#r "nuget: Templaty, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Templaty@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Templaty&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Templaty&version=1.0.1
                    
Install as a Cake Tool

Templaty - .net library for templating

1. Introduction

Templaty is a lightweight .net library based on Scriban library providing developers easy way to realize flexible templates feature into projects.

1.1 Use cases

  1. E-mailing message templates;
  2. Localization message templates;
  3. Text messages templates;

1.2 Concepts

Templaty is simple. It operates a few several concepts:

  1. Content - template content / message content;
  2. Template object - a class or record that has [Template.Source(...)] attribute and implements ITemplate interface;
  3. Store - service implements ITemplateContentStore where Content is storing;
  4. Loader - service that realizes Content loading;
  5. Builder - main service builds result by Template object and Content;

2. Quick start

First of all, you need to import Templaty as package;

dotnet add package Templaty

2.1 ASP.NET

To use Templaty into ASP.NET projects just using into Startup or Program:

builder.Services.UseTemplaty(configator => configator.AddResourceStoreAssembly(typeof(Program).Assembly));

By default Templaty provides only ResourceTemplateContentStore that registering while UseTemplate executing. It allows you to use embedded resource files as templates and use it around project;

2.2 Make your own template

Let's look at an example into project Templaty.Simple (see Samples).

  1. Create template content file (example: WheaterDistribution.txt);
Date: {{date}}
Temperature: {{temperature}} °C
Summary: {{summary}}
  1. Mark WheaterDistribution.txt as Embedded Resource (see: Build actions);

  2. Create a model with data WheaterDistributionTemplate;

[Template.Source("Templaty.Simple.Wheater.Notifications.WheaterDistribution.txt", Template.StoreType.Resources)]
internal sealed record WheaterDistributionTemplate(DateOnly Date, int Temperature, string? Summary) : ITemplate;
  1. Use ITemplateBuilder to get end-message from template;
var template = new WheaterDistributionTemplate(
    DateOnly.FromDateTime(DateTime.Now.AddDays(2)),
    Random.Shared.Next(-20, 55),
    Summaries[Random.Shared.Next(Summaries.Length)]
 );

var result = await _templateBuilder.LoadAndBuild(template);

Result will be something like that:

Date: 16.03.2025
Temperature: 8 °C
Summary: Balmy

3. Own store

Templaty allows you to make your own store that will contains template content whenever you want.

  1. Create new class that implements ITemplateContentStore;
internal sealed class LocalizableResourceStore : ITemplateContentStore
{
    private readonly IStringLocalizer<Templates> _stringLocalizer;

    public LocalizableResourceStore(IStringLocalizer<Templates> stringLocalizer)
    {
        _stringLocalizer = stringLocalizer;
    }

    public Template.StoreType Type => Template.StoreType.Localizations;

    public Task<string> GetContent(string path, CancellationToken cancellationToken = default)
    {
        var localizedString = _stringLocalizer.GetString(path);

        if (localizedString.ResourceNotFound)
        {
            throw new TemplateContentMissedException(path, Type, $"Localization '{path}' was not found.");
        }
        else if (string.IsNullOrWhiteSpace(localizedString))
        {
            return Task.FromResult(path);
        }

        return Task.FromResult(localizedString.Value);
    }
}
  1. Create new class that implements ITemplateContentStoreFactory;
internal sealed class LocalizableResourceStoreFactory(IStringLocalizer<Templates> stringLocalizer) : ITemplateContentStoreFactory
{
    public Template.StoreType Type => Template.StoreType.Localizations;

    public string Name => "default";

    public ITemplateContentStore Create() => new LocalizableResourceStore(stringLocalizer);
}
  1. Register created service-factory as ITemplateContentStoreFactory or configurator method .AddStore();
builder.Services.AddSingleton<LocalizableResourceStoreFactory>();
builder.Services.UseTemplaty(
    configator => configator
        .AddResourceStoreAssembly(typeof(Program).Assembly)
        .AddStore(x => x.GetRequiredService<LocalizableResourceStoreFactory>())
);
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Templaty:

Package Downloads
Templaty.Postgres

Library with Postgres store for Templaty

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 129 7/14/2025
1.0.0 168 4/9/2025