Backport several changes to flask-template from the api repository.