please use the new Template library to do this, this is safer, easier to use, easier to maintain, easier to read.

see for example https://github.com/mozilla-b2g/gaia/blob/master/apps/sms/js/attachment.js#L123-L130