Hyperbee.Templating 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Hyperbee.Templating --version 1.0.2
                    
NuGet\Install-Package Hyperbee.Templating -Version 1.0.2
                    
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="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hyperbee.Templating" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Hyperbee.Templating" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Hyperbee.Templating --version 1.0.2
                    
#r "nuget: Hyperbee.Templating, 1.0.2"
                    
#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.
#addin nuget:?package=Hyperbee.Templating&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Hyperbee.Templating&version=1.0.2
                    
Install as a Cake Tool

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.Json, refer to the documentation for detailed instructions and examples.

Install via NuGet:

dotnet add package Hyperbee.Template

Usage

Basic Variable Substitution

You can use the TemplateParser to perform basic variable substitutions.

var parser = new TemplateParser
{
    Tokens =
    {
        ["name"] = "me"
    }
};

var template = "hello {{name}}.";

var result = parser.Render(template);
Console.WriteLine(result); // Output: hello me.

Token Nesting

Token values can contain other tokens.

var parser = new TemplateParser
{
    Tokens =
    {
        ["fullname"] = "{{first}} {{last}}",
        ["first"] = "Hari",
        ["last"] = "Seldon"
    }
};

var template = "hello {{fullname}}.";

var result = parser.Render(template);
Console.WriteLine(result); // Output: hello Hari Seldon.

Inline Token Definitions

You can define tokens inline within a template. Inline tokens must be defined before they are referenced.

var template = """{{identity:"me"}} hello {{identity}}.""";

var result = parser.Render(template);
Console.WriteLine(result); // Output: hello me.

Conditional Tokens

You can use conditional tokens to control the flow based on conditions.

var parser = new TemplateParser
{
    Tokens =
    {
        ["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
{
    Tokens =
    {
        ["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.

Inline Token Definitions

var template = """{{identity:"me"}} hello {{identity}}.""";

var result = parser.Render(template);
Console.WriteLine(result); // Output: hello me.
var template = """{{identity:{{x => "me"}} }} hello {{identity}}.""";

var result = parser.Render(template);
Console.WriteLine(result); // Output: hello me.

While Loop

You can use a while loop to repeat a block of text while a condition is true.

var parser = new TemplateParser
{
    Tokens =
    {
        ["counter"] = 0
    }
};

var template = "{{while x => int.Parse(x.counter) < 3}}{{counter}}{{counter:{{x => int.Parse(x.counter) + 1}}}}{{/while}}";

var result = parser.Render(template);
Console.WriteLine(result); // Output: 012. 

CLR Method Invocation

You can invoke CLR methods within the template.

var parser = new TemplateParser
{
    Tokens =
    {
        ["name"] = "me"
    }
};

var template = "hello {{x => x.name.ToUpper()}}.";

var result = parser.Render(template);
Console.WriteLine(result); // Output: hello ME.

User-Defined Methods

You can define custom methods and use them within the template.

var parser = new TemplateParser
{
    Methods =
    {
        ["MyUpper"] = args => ((string)args[0]).ToUpper()
    },
    Tokens =
    {
        ["name"] = "me"
    }
};

var template = "hello {{x => x.name.MyUpper()}}.";

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 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 was computed.  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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Hyperbee.Templating:

Package Downloads
Hyperbee.Templating.Provider.XS

Adds an ITokenExpressionProvider powder by Hyperbee.XS

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.2.0 98 5/9/2025
3.1.0 144 5/1/2025
3.0.2 190 4/14/2025
3.0.2-develop.250414130051 164 4/14/2025
3.0.1 126 2/17/2025
3.0.1-develop.250217125033 73 2/17/2025
3.0.0 110 12/19/2024
3.0.0-develop.241219164731 65 12/19/2024
2.0.0 193 8/23/2024
1.0.3 151 8/13/2024
1.0.2 139 8/13/2024
1.0.1 155 4/11/2024
1.0.0 146 4/9/2024