Hyperbee 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, and does not rely on any external dependencies.
- Variable substitution syntax engine
- Value replacements
- Expression replacements
- Token nesting
- Conditional tokens
- Conditional flow
- Iterators
- User-defined methods
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 parser = new TemplateParser
{
Variables =
{
["name"] = "me"
}
};
var template = "hello {{name}}.";
var result = parser.Render(template);
Console.WriteLine(result); // Output: hello me.
var parser = new TemplateParser
{
Variables =
{
["name"] = "me"
}
};
var template = "hello {{x => x.name.ToUpper()}}.";
var result = parser.Render(template);
Console.WriteLine(result); // Output: hello ME.
Token values can contain other tokens.
var parser = new TemplateParser
{
Variables =
{
["fullname"] = "{{first}} {{last}}",
["first"] = "Hari",
["last"] = "Seldon"
}
};
var template = "hello {{fullname}}.";
var result = parser.Render(template);
Console.WriteLine(result); // Output: hello Hari Seldon.
You can use conditional tokens to control the flow based on conditions.
var parser = new TemplateParser
{
Variables =
{
["condition"] = "true",
["name"] = "me"
}
};
var template = "{{#if condition}}hello {{name}}.{{/if}}";
var result = parser.Render(template);
Console.WriteLine(result); // Output: hello me.
var parser = new TemplateParser
{
Variables =
{
["condition"] = "false",
["name1"] = "me",
["name2"] = "you",
}
};
var template = "hello {{#if condition}}{{name1}}{{else}}{{name2}}{{/if}}.";
var result = parser.Render(template);
Console.WriteLine(result); // Output: hello you.
You can use a while statement to repeat a block of text while a condition is true.
var parser = new TemplateParser
{
Variables =
{
["counter"] = "0"
}
};
var template = "{{while x => x.counter<int> < 3}}{{counter}}{{counter:{{x => x.counter<int> + 1}}}}{{/while}}";
var result = parser.Render(template);
Console.WriteLine(result); // Output: 012.
You can invoke methods within token expressions.
var options = new TemplateOptions()
.AddVariable("name", "me")
.AddMethod("ToUpper").Expression<string,string>( value => value.ToUpper() );
var parser = new TemplateParser( options );
var template = "hello {{x => x.ToUpper( x.name )}}.";
var result = parser.Render(template);
Console.WriteLine(result); // Output: hello ME.
Special thanks to:
- Just The Docs for the documentation theme.
We welcome contributions! Please see our Contributing Guide for more details.