I would suggest looking into a template engine for this problem. To me it's not exactly a backend but more of a grey area. Something like Moustache OR smarty would be perfect for you.
Simply put, i agree with the other posters that this is not reasonable to achieve on the client side.