I am currently creating this small template engine. It takes a string containing the template in the parameter, and a dictionary of "tags, values" to populate the template.
In the engine, I have no idea about the tags that will be in the template, and those that will not.
I am currently repeating (foreach) on dictionnary, analyzing my string that I entered in the line builder, and replacing the tags in the template with their corresponding value.
Is there a more efficient / convenient way to do this? I know that the main drawback here is that the string constructor is parsed completely every time for each tag, which is pretty bad ...
(I also check, although not included in the sample, after the process, when my template no longer contains a tag. All of them are formed identically: @@ tag @@)
StringBuilder outputBuilder = new StringBuilder(template);
foreach (string tag in tagsValueCorrespondence.Keys)
{
outputBuilder.Replace(tag, tagsValueCorrespondence[tag]);
}
template = outputBuilder.ToString();
Answers:
@Marc:
string template = "Some @@foobar@@ text in a @@bar@@ template";
StringDictionary data = new StringDictionary();
data.Add("foo", "value1");
data.Add("bar", "value2");
data.Add("foo2bar", "value3");
: " value2"
: " @@foobar @@text value2"
qwerty