From the looks of it, HTML::Template does exactly what you want (replace any variables).
Try it, it's better than reinventing an already rolling wheel :)
I'd also look at Template(::Toolkit), which isn't just about HTML, but is a very powerful preprocessor language. Very Big e-Commerce sites use this code.