Fix issue #8 (undoredo).

This commit adds a proper undo/redo mechanism for switching templates.