PosInformatique.Foundations.Text.Templating.Scriban
1.6.0-rc.2
Prefix Reserved
dotnet add package PosInformatique.Foundations.Text.Templating.Scriban --version 1.6.0-rc.2
NuGet\Install-Package PosInformatique.Foundations.Text.Templating.Scriban -Version 1.6.0-rc.2
<PackageReference Include="PosInformatique.Foundations.Text.Templating.Scriban" Version="1.6.0-rc.2" />
<PackageVersion Include="PosInformatique.Foundations.Text.Templating.Scriban" Version="1.6.0-rc.2" />
<PackageReference Include="PosInformatique.Foundations.Text.Templating.Scriban" />
paket add PosInformatique.Foundations.Text.Templating.Scriban --version 1.6.0-rc.2
#r "nuget: PosInformatique.Foundations.Text.Templating.Scriban, 1.6.0-rc.2"
#:package PosInformatique.Foundations.Text.Templating.Scriban@1.6.0-rc.2
#addin nuget:?package=PosInformatique.Foundations.Text.Templating.Scriban&version=1.6.0-rc.2&prerelease
#tool nuget:?package=PosInformatique.Foundations.Text.Templating.Scriban&version=1.6.0-rc.2&prerelease
PosInformatique.Foundations.Text.Templating.Scriban
Introduction
This package provides a simple way to generate text using Scriban templates.
You define a Scriban template string with mustache-style syntax ({{ }}) and the library renders it to a
TextWriter by using a ScribanTextTemplate<TModel> implementation.
The model properties are automatically exposed to the template.
Install
You can install the package from NuGet:
dotnet add package PosInformatique.Foundations.Text.Templating.Scriban
Features
- Render text from Scriban templates using mustache-style syntax
- Strongly-typed model with automatic property exposure
- Supports both POCO objects and
ExpandoObject - Simple integration with
ITextTemplateRenderContext - Lightweight and fast text generation
Basic usage
1. Create a model
Define a model class with the data you want to render:
public class EmailModel
{
public string Name { get; set; }
public string Email { get; set; }
public DateTime Date { get; set; }
}
2. Create a Scriban template
Define a Scriban template string using mustache-style syntax:
var templateContent = @"
Hello {{ Name }},
Your email address is: {{ Email }}
Today is: {{ Date }}
Thank you!
";
3. Use ScribanTextTemplate<TModel>.RenderAsync()
Create a ScribanTextTemplate<TModel> instance and call RenderAsync():
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using PosInformatique.Foundations.Text.Templating;
using PosInformatique.Foundations.Text.Templating.Scriban;
// Example of a simple ITextTemplateRenderContext implementation
public class TextTemplateRenderContext : ITextTemplateRenderContext
{
public TextTemplateRenderContext(IServiceProvider serviceProvider)
{
this.ServiceProvider = serviceProvider;
}
public IServiceProvider ServiceProvider { get; }
}
public static class ScribanTemplateSample
{
public static async Task GenerateAsync()
{
var templateContent = @"
Hello {{ Name }},
Your email address is: {{ Email }}
Today is: {{ Date }}
Thank you!
";
// Create the Scriban text template
var template = new ScribanTextTemplate<EmailModel>(templateContent);
// Create a model
var model = new EmailModel
{
Name = "John Doe",
Email = "john.doe@example.com",
Date = DateTime.UtcNow
};
// Build the context (can use an empty IServiceProvider if not needed)
var context = new TextTemplateRenderContext(serviceProvider: null);
using var writer = new StringWriter();
// Render the template
await template.RenderAsync(model, writer, context, CancellationToken.None);
var result = writer.ToString();
Console.WriteLine(result);
}
}
Output:
Hello John Doe,
Your email address is: john.doe@example.com
Today is: 2025-01-16 10:30:00
Thank you!
4. Using ExpandoObject
You can also use ExpandoObject for dynamic models:
dynamic model = new ExpandoObject();
model.Name = "Alice";
model.Email = "alice@example.com";
model.Date = DateTime.UtcNow;
var templateContent = "Hello {{ Name }}, your email is {{ Email }}.";
var template = new ScribanTextTemplate<ExpandoObject>(templateContent);
using var writer = new StringWriter();
await template.RenderAsync(model, writer, context, CancellationToken.None);
Console.WriteLine(writer.ToString());
// Output: Hello Alice, your email is alice@example.com.
Scriban syntax
Scriban supports a rich template syntax. Here are some common examples:
Variables
Hello {{ Name }}!
Conditionals
{{ if IsActive }}
User is active
{{ else }}
User is inactive
{{ end }}
Loops
{{ for item in Items }}
- {{ item.Name }}
{{ end }}
Filters
{{ Name | upcase }}
{{ Date | date.to_string '%Y-%m-%d' }}
For more details, see the Scriban documentation.
Trimming and Native AOT
The ScribanTextTemplate<TModel> implementation access the members of the TModel model through reflection.
The trimmer cannot detect this usage statically, so when your application is published with trimming or Native AOT enabled,
the model type and its members can be removed, which causes errors at runtime.
The TModel type passed to RenderAsync() must therefore be preserved by the application using the DynamicDependencyAttribute.
Apply it on the method (or the class) which calls RenderAsync(), and reference the model type:
using System.Diagnostics.CodeAnalysis;
public class MyEmailModel
{
public string UserName { get; set; }
public DateTime Now { get; set; }
}
public class EmailSender
{
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(MyEmailModel))]
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "MyEmailModel is preserved by the DynamicDependency attribute.")]
public async Task SendAsync(TextTemplate<MyEmailModel> template, MyEmailModel model, TextWriter writer, ITextTemplateRenderContext context)
{
await template.RenderAsync(model, writer, context);
}
}
Notes:
- Every type of the model graph accessed by the template (nested objects, items of collections,...) must be preserved as well, by adding one
DynamicDependencyAttributeper type. RenderAsync()is annotated withRequiresUnreferencedCode: without theUnconditionalSuppressMessage(orRequiresUnreferencedCodeon your own method), the trimming analyzer raises theIL2026warning on the call.
Links
| Product | Versions 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 is compatible. 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 is compatible. 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. net11.0 is compatible. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0)
- PosInformatique.Foundations.Text.Templating (>= 1.6.0-rc.2)
- Scriban (>= 7.2.2)
-
net11.0
- PosInformatique.Foundations.Text.Templating (>= 1.6.0-rc.2)
- Scriban (>= 7.2.2)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- PosInformatique.Foundations.Text.Templating (>= 1.6.0-rc.2)
- Scriban (>= 7.2.2)
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- PosInformatique.Foundations.Text.Templating (>= 1.6.0-rc.2)
- Scriban (>= 7.2.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.6.0-rc.2 | 45 | 11/18/2026 |
| 1.6.0-rc.1 | 51 | 11/17/2026 |
| 1.5.0 | 92 | 11/10/2026 |
| 1.5.0-rc.1 | 51 | 11/10/2026 |
| 1.4.0 | 89 | 11/9/2026 |
| 1.4.0-rc.5 | 57 | 11/7/2026 |
| 1.4.0-rc.4 | 64 | 10/24/2026 |
| 1.4.0-rc.3 | 93 | 10/14/2026 |
| 1.4.0-rc.2 | 65 | 10/6/2026 |
| 1.4.0-rc.1 | 73 | 10/5/2026 |
| 1.3.0 | 148 | 10/2/2026 |
| 1.2.0 | 155 | 8/18/2026 |
| 1.1.0 | 161 | 3/30/2026 |
1.4.0
- Upgrade the [Scriban](Scriban) dependency to version 7.2.2 at minimum to fix security vulnerabilities.
1.1.0
- Upgrade the [Scriban](Scriban) dependency to version 7.0.0 to fix security vulnerabilities.
- [Scriban Affected by Memory Exhaustion (OOM) via Unbounded String Generation (Denial of Service)](https://github.com/advisories/GHSA-5rpf-x9jg-8j5p)
- [Scriban: Sandbox escape due to TypedObjectAccessorcache bypassing MemberFilter after TemplateContext reuse](https://github.com/advisories/GHSA-5wr9-m6jw-xx44)
- [Scriban: Denial of Service via Unbounded Cumulative Template Output Bypassing LimitToString](https://github.com/advisories/GHSA-m2p3-hwv5-xpqw)
- [Scriban has Multiple Denial-of-Service Vectors via Unbounded Resource Consumption During Expression Evaluation](https://github.com/advisories/GHSA-xw6w-9jjh-p9cr)
1.0.0
- Initial release with the Scriban Text Templating feature.