jehugaleahsa / mustache-sharp

An extension of the mustache text template engine for .NET.
The Unlicense
306 stars 78 forks source link

Add support for escaped strings - closes #16 #36

Open squarewave opened 9 years ago

squarewave commented 9 years ago

This is in line with standard mustache/handlebars. Double-mustache tags will be encoded with the provided IStringEncoder (defaults to PlainTextEncoder), while triple-mustache tags will be left as-is. This should be 100% backwards-compatible and passes all existing unit tests without modification.

Using HTML encoding looks like this, as defined in the test cases:

var compiler = new FormatCompiler(new HtmlEncoder());
const string format = @"Hello, {{this}}";
var generator = compiler.Compile(format);
const string expected = @"Hello, <b>Bob</b>";
var actual = generator.Render("<b>Bob</b>");
Assert.AreEqual(expected, actual);

Literal tagging looks like this:

var compiler = new FormatCompiler(new HtmlEncoder());
const string format = @"Hello, {{{this}}}";
var generator = compiler.Compile(format);
const string expected = @"Hello, <b>Bob</b>";
var actual = generator.Render("<b>Bob</b>");
Assert.AreEqual(expected, actual);