Library for fast templates creation
$ dotnet add package Templaty.AbstractionsTemplaty is a lightweight .net library based on Scriban library providing developers easy way to realize flexible templates feature into projects.
Templaty is simple. It operates a few several concepts:
class or record that has [Template.Source(...)] attribute and implements ITemplate
interface;ITemplateContentStore where Content is storing;First of all, you need to import Templaty as package;
dotnet add package Templaty
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;
Let's look at an example into project Templaty.Simple (see Samples).
WheaterDistribution.txt);Date: {{date}}
Temperature: {{temperature}} °C
Summary: {{summary}}
Mark WheaterDistribution.txt as Embedded Resource (see: Build actions);
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;
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
Templaty allows you to make your own store that will contains template content whenever you want.
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);
}
}
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);
}
ITemplateContentStoreFactory or configurator method .AddStore();builder.Services.AddSingleton<LocalizableResourceStoreFactory>();
builder.Services.UseTemplaty(
configator => configator
.AddResourceStoreAssembly(typeof(Program).Assembly)
.AddStore(x => x.GetRequiredService<LocalizableResourceStoreFactory>())
);