TinyTemplatePattern

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Text;

public class Program
{
	const string _template = "The quick {{Color}} {{Animal}} jumped over the lazy dog";
	
	public static void Main()
	{
		var properties = new Dictionary<string, string>
		{
			{ "Color", "brown" },
			{ "Animal", "fox" }
		};
		
		Console.WriteLine(ApplyTemplate(_template, properties));
	}

	public static string ApplyTemplate(string template, Dictionary<string, string> properties)
	{
		var result = new StringBuilder();
		
		var templateSections = template.Split(new[] { "{{" }, StringSplitOptions.None);
		
		result.Append(templateSections[0]);
		
		for (int i = 1, length = templateSections.Length; i < length; i++)
		{
			var sectionResult = templateSections[i].Split(new[]	{ "}}" }, 2, StringSplitOptions.None);
			result.Append(properties[sectionResult[0]]);
			result.Append(sectionResult[1]);
		}

		return result.ToString();
	}
}
;