Hyperbee.Templating
1.0.1
See the version list below for details.
dotnet add package Hyperbee.Templating --version 1.0.1
NuGet\Install-Package Hyperbee.Templating -Version 1.0.1
<PackageReference Include="Hyperbee.Templating" Version="1.0.1" />
paket add Hyperbee.Templating --version 1.0.1
#r "nuget: Hyperbee.Templating, 1.0.1"
// 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 | Versions 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. |
-
net8.0
- Hyperbee.Resources (>= 1.0.0)
- Microsoft.CodeAnalysis.CSharp.Scripting (>= 4.9.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.