Add `compile` templatetag

Also some refactoring and bugfixes.