Bug 49916: Avoid crash in TemplateHandler nested ref code

Change-Id: I2ba48bd0201bd9d782f7cf19d73c7b1025ab63b2