Oh, wrote this a while back - https://github.com/guardian/frontend/blob/master/common/app/assets/javascripts/utils/template.js