Hafr 2.1.1
dotnet add package Hafr --version 2.1.1
NuGet\Install-Package Hafr -Version 2.1.1
<PackageReference Include="Hafr" Version="2.1.1" />
paket add Hafr --version 2.1.1
#r "nuget: Hafr, 2.1.1"
// Install Hafr as a Cake Addin #addin nuget:?package=Hafr&version=2.1.1 // Install Hafr as a Cake Tool #tool nuget:?package=Hafr&version=2.1.1
<div align="center">
<img src="logo.png" width="320" height="320">
<p>
<b>A tiny templating language for writing email generation conventions.</b>
</p>
</div>
Writing a Template
The Template Structure
A Hafr template consists of two different parts: text and holes. Text is just literal text that will be output verbatim when evaluating the template against a model. A hole will be filled in based on its contents and the model during evaluation. Here's an example:
Hello {name}!
The first and last parts, Hello
and !
are literal text, while {name}
is a hole.
Inside holes, you can access public properties of the model you're evaluating against. If you have the following class:
public record Person(string Name);
And evaluate the template against the following instance:
var person = new Person("John Doe");
The template above will yield:
Hello John Doe!
Any public property of the model will be exposed to the template.
Calling Functions
Hafr has a few built-in methods that you can call to transform properties inside holes:
Method | Description |
---|---|
split |
Takes a separator argument to split its input into separate parts. |
join |
Takes a separator argument to join several parts into one. |
substr |
Takes a count argument to extract a specified number of characters for each part. |
skip |
Takes a count argument to skip a specified number of parts. |
take |
Takes a count argument to pick a specified number of parts. |
replace |
Takes a string to replace and its replacement value. |
reverse |
Reverses a string or a list of strings. |
upper |
Converts a string to uppercase. |
lower |
Converts a string to lowercase. |
trim |
Trims whitespace from the start and end of a string. |
truncate |
Truncates a string to the specified max length. |
These methods can be called in one of two ways; using C-style function calls:
Hello {join(split(name, ' '), '.')}!
...or using piping:
Hello {name | split(' ') | join('.')}!
Parsing a Template
To parse a template, use the Parser.TryParse
method:
var template = "{firstName | split(' ') | join('.')}.{lastName}@company.com";
if (Parser.TryParse(template, out var expression, out var errorMessage, out var errorPosition))
{
// Parsing succeeded, it's safe to access the expression in here...
}
else
{
// Parsing failed. You can use errorMessage and errorPosition in here...
}
Evaluating a Template
After the template has been successfully parsed, you can use it to evaluate against an input model:
public record Person(string FirstName, string LastName);
var model = new Person("John Michael", "Doe");
var result = expression.Evaluate(model).ToLower(); // john.michael.doe@company.com
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Superpower (>= 3.0.0)
-
net6.0
- Superpower (>= 3.0.0)
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 |
---|---|---|
2.1.1 | 196 | 8/7/2023 |
2.1.0 | 364 | 12/6/2022 |
2.0.0 | 461 | 5/24/2022 |
1.5.1 | 454 | 5/24/2022 |
1.5.0 | 410 | 5/24/2022 |
1.4.0 | 435 | 5/10/2022 |
1.3.0 | 359 | 10/21/2021 |
1.2.1 | 2,130 | 5/31/2021 |
1.2.0 | 337 | 5/31/2021 |
1.1.0 | 351 | 4/30/2021 |
1.0.1 | 315 | 4/29/2021 |
1.0.0 | 304 | 4/29/2021 |
1.0.0-beta.2 | 149 | 4/28/2021 |
1.0.0-beta.1 | 166 | 4/27/2021 |