Mustache

.NET Standard and Portable Class Library implementations of Mustache logic-less templates

View the Project on GitHub DanieleScipioni/Mustache

Mustache.NETStandard & Mustache.PCL - Logic-less templates for .NET

These libraries are implementations of Mustache logic-less templates specifications.

Mustache.NETStandard is a .NETStandard Class Library for .NETStandard 2.0.

Mustache.PCL is a .NET Portable Class Library for

Mustache documentation and specifications can be found at http://mustache.github.io.

Compliance to Mustache specifications

This implementation is compliant to version 1.2.2 (at today 2022.08.10 this is the last version) of Mustache specifications except for the optional module lambdas, inheritance and dynamic-names. Specifications are available at https://github.com/mustache/spec.

NuGet

Mustache.NETStandard is availabe using NuGet at https://www.nuget.org/packages/Mustache.NETStandard.

Mustache.PCL is availabe using NuGet at https://www.nuget.org/packages/Mustache.PCL.

How to use the library

Here is a code sample to explain how to use this library

const string templateString = @"Hello {{Name}}
You have just won {{Value}} {{Currency}}!
{{#InCa}}
Well, {{TaxedValue}} {{Currency}}, after taxes.
{{/InCa}}";

var data = new
{
    Name = "Chris",
    Value = 10000,
    TaxedValue = 6000,
    Currency = "dollars",
    InCa = true
};

string result = Template.Compile(templateString).Render(data);

The value of result is

Hello Chris
You have just won 10000 dollars!
Well, 6000 dollars, after taxes.

It is possible to use Dictionaries too:

var data = new Dictionary<string, object>
{
    {"Name", "Chris"},
    {"Value", "10000"},
    {"TaxedValue", 6000},
    {"Currency", "dollars"},
    {"InCa", true}
};

If you need partials use method Template.Render(object, Dictionary<string, string>), example:

var data = new
{
    Name = "Chris",
    Value = 10000,
    TaxedValue = 6000,
    Currency = "dollars",
    InCa = true
};

var partials = new Dictionary<string, string> { { "partial", "Well, {{TaxedValue}} {{Currency}}, after taxes.\r\n" } };

const string templateString = @"Hello {{Name}}
You have just won {{Value}} {{Currency}}!
{{#InCa}}
{{>partial}}
{{/InCa}}
";

 string result = Template.Compile(templateString).Render(data, partials);