Check widget template context still exists before using it

Just check that the associated template context for a widget still exists
before trying to execute a given callback on it.

Fixes #1232