The logical explanation is that you are running code from a template which you are then removing from the document to attach another template. The code will undoubtedly stop at that point.