Did something similar. But instead of adding plain-text to HTML output, wrote a DOM parser in C that loads a HTML template and then adds the response into proper place of the DOM tree.
Further you can read back previously added contents and make additional chagnes based on those. Like summing up table columns, or adding links to the first column values, or removing or fixing links based on authorization, to name a few.
Syntax aware template languages are a thing in the web space. They aren't the most popular but security people often like them because they lead to less mistakes. Some examples include soy, latte, and hack's XHP.
Found the old code on disk, snapshot here. The DOM parser, manipulator and free was 240 LOC.