snipervld.Stubble.Helpers 2.0.2

dotnet add package snipervld.Stubble.Helpers --version 2.0.2                
NuGet\Install-Package snipervld.Stubble.Helpers -Version 2.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="snipervld.Stubble.Helpers" Version="2.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add snipervld.Stubble.Helpers --version 2.0.2                
#r "nuget: snipervld.Stubble.Helpers, 2.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.
// Install snipervld.Stubble.Helpers as a Cake Addin
#addin nuget:?package=snipervld.Stubble.Helpers&version=2.0.2

// Install snipervld.Stubble.Helpers as a Cake Tool
#tool nuget:?package=snipervld.Stubble.Helpers&version=2.0.2                

Stubble Extensions - Helpers (+ Section Helpers) Nuget

Stubble helpers is an opinionated method of registering helpers with the Stubble renderer to call certain methods with arguments while rendering your templates much like Handlebars.js helpers and block helpers.

To get started with helpers, include the package from nuget and register your helpers like so.

var culture = new CultureInfo("en-GB");
var helpers = new Helpers()
    .Register("FormatCurrency", (HelperContext context, decimal count) => count.ToString("C", culture));
var sectionHelpers = new SectionHelpers()
    .Register("IfEqualsTo5", (HelperSectionContext context, decimal count) => count == 5);

var stubble = new StubbleBuilder()
    .Configure(conf =>
        conf
            .AddHelpers(helpers)
            .AddSectionHelpers(sectionHelpers))
    .Build();

var result = stubble.Render("{{FormatCurrency Count}}", new { Count = 100.26m });

Assert.Equal("£100.26", result);

result = stubble.Render("{{#IfEqualsTo5 Count}}{{Count}} equals to 5{{/IfEqualsTo5}}", new { Count = 5 });

Assert.Equal("5 equals to 5", result);

result = stubble.Render("{{^IfEqualsTo5 Count}}{{Count}} doesn't equal to 5{{/IfEqualsTo5}}", new { Count = 6 });

Assert.Equal("6 doesn't equal to 5", result);

For more advanced cases you can use the HelperContext or the HelperSectionContext to get access to values in your current context in a strongly typed manner like the following:

var helpers = new Helpers()
    .Register("PrintListWithComma", (context) => string.Join(", ", context.Lookup<int[]>("List")));

var builder = new StubbleBuilder()
    .Configure(conf => conf.AddHelpers(helpers))
    .Build();

var res = builder.Render("List: {{PrintListWithComma}}", new { List = new[] { 1, 2, 3 } });

Assert.Equal("List: 1, 2, 3", res);

You can also have static arguments in your template that will be parsed into your helper. There are some caveats to this which i'll note below the example:

var culture = new CultureInfo("en-GB");
var helpers = new Helpers()
    .Register("FormatCurrency", (HelperContext context, decimal count) => count.ToString("C", culture));

var stubble = new StubbleBuilder()
    .Configure(conf => conf.AddHelpers(helpers))
    .Build();

var result = stubble.Render("{{FormatCurrency 10}}", new { });

Assert.Equal("£10.00", result);

Cavets:

  • The type should match or be convertable to the argument type
  • If you're writing a constant string as an argument then it should be escaped with quotes either " or '. Quoted strings are treated as verbatim and will not be attempted to be looked up in the context however their type will still be converted
  • If you have a quote in your string for example It's then you can escape it with a / like so: It/'s

Argument Type Converting

The helpers will try to be as smart and convert the parameters types if you're convertable or able to be used as that value. For example string->int.

Limitations

This uses the C# Func delegates for registering these functions and so you're limited to 15 parameters but we feel this is a pretty fair limitation and anything more and you should be preprocessing your data before rendering.

There is also no async support inside your helpers for the same reasons since you should be preprocessing your data before rendering in this case.

Differences From Stubble.Helpers

  • Added section helpers ("block helpers" in Handlebars.js)
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
2.0.2 551 11/14/2021
2.0.1 381 10/31/2021
2.0.0 358 10/31/2021