Added targetRef support to templates. Contributed by Jeremy Gustie.