Refactor template_for logic into one place
Add support for custom template for errored builds