I actually built something similar in Clojure for a client, if it where not for a client I would share it with you, but the basics where pretty easy, what I did was I used the HTML data attributes as my templating instructions so I would add a attribute like <div data-cms-template="header"> I then used a parser to look for the attributes and replace them with the other template file content that where specified in a config.xml file. It was simple and it worked well. I really wish the big CMS's would convert over to using the data attributed as opposed to each using some kind of proprietary custom tags. IT would be really nice if they standardized the attribute names so that at least the basic templates where cross CMS compatible.