Hyperbee.Templating 1.0.1

dotnet add package Hyperbee.Templating --version 1.0.1
NuGet\Install-Package Hyperbee.Templating -Version 1.0.1
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.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hyperbee.Templating --version 1.0.1
#r "nuget: Hyperbee.Templating, 1.0.1"
#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=1.0.1

// Install Hyperbee.Templating as a Cake Tool
#tool nuget:?package=Hyperbee.Templating&version=1.0.1

Hyperbee.Templating

A simple templating engine supporting value replacements, code expressions, token nesting, in-line definitions, and if else conditions.

Syntax

The templating engine supports a simple syntax.

Token Values

Token values are either simple substitutions or expression results:

  • {{token}}
  • {{context => context.token}}
  • {{context => context.token.ToUpper()}}

Token Conditions

Token conditions are either simple truthy tokens or expression results:

  • {{if token}} _truthy_content_ {{/if}}
  • {{if !token}} _falsy_content_ {{/if}}
  • {{if token}} _true_content_ {{else}} _false_content_ {{/if}}
  • {{if context => context.token == "test"}} _true_content_ {{/if}}
  • {{if context => context.token == "test"}} _true_content_ {{else}} _false_content_ {{/if}}

Methods

The templating engine supports two kinds of method evaluations; strongly typed CLR class methods through the Roslyn compiler, and dynamic methods in the form of Func expressions that are runtime bound to the expression argument context.

\\ example of CLR String.ToUpper

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

var result = parser.Render( $"hello {{x => x.name.ToUpper()}}." );
\\ example of a Func expression MyUpper

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

var result = parser.Render( $"hello {{x => x.MyUpper( x.name )}}." );

Token Nesting

Token values can contain tokens.

\\ example of token nesting

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

var result = parser.Render( $"hello {{fullname}}." );

Inline Token Definitions

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

{{identity:"me"}}
hello {{identity}}.
{{identity:{{x=> "me"}} }}
hello {{identity}}.

TODO

It would be useful to support simple enumerations.

{{each regex}}
    {{@value}} {{@index}}
{{/each}}

{{each x => IEnumeration<string>}}
    {{ x => x().value + x().index }}
{{/each}}
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. 
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
1.0.1 97 4/11/2024
1.0.0 88 4/9/2024