A lightweight templating and variable substitution syntax engine. The library supports value replacements, code expressions, token nesting, in-line definitions, conditional flow, and looping.
$ dotnet add package Hyperbee.TemplatingHyperbee Templating is a lightweight templating and variable substitution syntax engine. The library supports value replacements, code expressions, token nesting, in-line definitions, conditional flow, and looping. It is designed to be lightweight and fast.
To get started with Hyperbee.Templating, refer to the documentation for detailed instructions and examples.
Install via NuGet:
dotnet add package Hyperbee.Templating
You can use the TemplateParser to perform variable substitutions.
var template = "hello {{name}}.";
var result = Template.Render(template, new()
{
Variables =
{
["name"] = "me"
}
});
Console.WriteLine(result); // Output: hello me.
var template = "hello {{x => x.name.ToUpper()}}.";
var result = Template.Render(template, new()
{
Variables =
{
["name"] = "me"
}
});
Console.WriteLine(result); // Output: hello ME.
Token values can contain other tokens.
var template = "hello {{fullname}}.";
var result = Template.Render(template, new()
{
Variables =
{
["fullname"] = "{{first}} {{last}}",
["first"] = "Hari",
["last"] = "Seldon"
}
});
Console.WriteLine(result); // Output: hello Hari Seldon.
You can use conditional tokens to control the flow based on conditions.
var template = "{{#if condition}}hello {{name}}.{{/if}}";
var result = Template.Render(template, new()
{
Variables =
{
["condition"] = "true",
["name"] = "me"
}
});
Console.WriteLine(result); // Output: hello me.
var template = "hello {{#if condition}}{{name1}}{{else}}{{name2}}{{/if}}.";
var result = Template.Render(template, new()
{
Variables =
{
["condition"] = "false",
["name1"] = "me",
["name2"] = "you",
}
});
Console.WriteLine(result); // Output: hello you.
You can use a while statement to repeat a block of text while a condition is true.
var template = "{{while x => x.counter<int> < 3}}{{counter}}{{counter:{{x => x.counter<int> + 1}}}}{{/while}}";
var result = Template.Render(template, new()
{
Variables =
{
["counter"] = "0"
}
});
Console.WriteLine(result); // Output: 012.
var template = "{{each n:x => x.list.Split( \",\" )}}World {{n}},{{/each}}";
var result = Template.Render(template, new()
{
Variables =
{
["list"] = "John,James,Sarah"
}
});
Console.WriteLine(result); // hello World John,World James,World Sarah,.
var template = "{{each n:x => x.Where( t => Regex.IsMatch( t.Key, \"people*\" ) ).Select( t => t.Value )}}hello {{n}}. {{/each}}";
var result = Template.Render(template, new()
{
Variables =
{
["people[0]"] = "John",
["people[1]"] = "Jane",
["people[2]"] = "Doe"
}
});
Console.WriteLine(result); // hello John. hello Jane. hello Doe.
You can invoke methods within token expressions.
var options = new TemplateOptions()
.AddVariable("name", "me")
.AddMethod("ToUpper").Expression<string,string>( value => value.ToUpper() );
var template = "hello {{x => x.ToUpper( x.name )}}.";
var result = Template.Render(template, options);
Console.WriteLine(result); // Output: hello ME.
Special thanks to:
We welcome contributions! Please see our Contributing Guide for more details.
See Todo