Hyperbee.Templating 3.0.0-develop.241219164731

This is a prerelease version of Hyperbee.Templating.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Hyperbee.Templating --version 3.0.0-develop.241219164731                
NuGet\Install-Package Hyperbee.Templating -Version 3.0.0-develop.241219164731                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hyperbee.Templating" Version="3.0.0-develop.241219164731" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hyperbee.Templating --version 3.0.0-develop.241219164731                
#r "nuget: Hyperbee.Templating, 3.0.0-develop.241219164731"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Hyperbee.Templating as a Cake Addin
#addin nuget:?package=Hyperbee.Templating&version=3.0.0-develop.241219164731&prerelease

// Install Hyperbee.Templating as a Cake Tool
#tool nuget:?package=Hyperbee.Templating&version=3.0.0-develop.241219164731&prerelease                

Hyperbee Templating

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.

Features

  • Variable substitution syntax engine
  • Value replacements
  • Expression replacements
  • Token nesting
  • Conditional tokens
  • Conditional flow
  • Iterators
  • User-defined methods

Getting Started

To get started with Hyperbee.Templating, refer to the documentation for detailed instructions and examples.

Install via NuGet:

dotnet add package Hyperbee.Templating

Basic Usage

Variable Substitution

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.

Expression Substitution

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 Nesting

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.

Conditional Tokens

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.

While Statement

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. 

Each Statement

var template = "{{each n:x => x.list.Split( \",\" )}}World {{n}},{{/each}}";

var parser = new TemplateParser
{
    Variables = { ["list"] = "John,James,Sarah" }
};

var result = parser.Render(template);
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 parser = new TemplateParser
{
    Variables = 
        {
            ["people[0]"] = "John",
            ["people[1]"] = "Jane",
            ["people[2]"] = "Doe"
        }
};

var result = parser.Render(template);
Console.WriteLine(result); // hello John. hello Jane. hello Doe. 

Methods

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.

Credits

Special thanks to:

Contributing

We welcome contributions! Please see our Contributing Guide for more details.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
3.0.0 72 12/19/2024
3.0.0-develop.241219164731 35 12/19/2024
2.0.0 161 8/23/2024
1.0.3 135 8/13/2024
1.0.2 123 8/13/2024
1.0.1 140 4/11/2024
1.0.0 122 4/9/2024