Move template logic to own file