Restricted templates

Closes #371

It appears there has never been a check for restricted templates. I added it, and I hope there is no other location than this 1 where a template might be set.

accidentally contains commits from another pull request