Move common template logic out to a new template() function that calls one of the existing template processors and then handles writing the result and copying permissions.