Template context

Resolves #1834.

Also needs a rebase or something.