Poor Man's Template Engine in C# Part 2

HtmlEncode and If

March 14, 2018 c#, template, template-engine

In our last episode we laid the groundwork for our simple template engine. In this episode we'll introduce our first 2 helper functions.

namespace Pmte { public delegate IEnumerable<string> Template<TData>(TData data); public static class TemplateExtensions { public static string Render(this IEnumerable<string> templateResult) => string.Join(Environment.NewLine, templateResult); public static string HtmlEncode(string input) => WebUtility.HtmlEncode(input); public static string If(bool condition, string trueString, string falseString = null) => condition ? trueString : falseString; } }

The first helper function HtmlEncode is fairly self explanitory. Use it to encode a string so that it is valid html.

If can be used inside an interpolated string to optionally include a string or include a different string depending on if condition is true or false.

using static Pmte.TemplateExtensions; public static class Templates { public static IEnumerable<string> Path(string[] paths) { string output = ""; for (int i = 0; i < paths.Length; i++) output += $"{paths[i]}{If(i < paths.Length - 1, HtmlEncode(" >"))}"; yield return output; } }

Next time we'll talk about including templates inside of other templates.