Fix templates relative lookup

When specified template name has more then one section relative lookup used only the last section of a name which was leading to improper behavior.

This is related to this issue:
https://github.com/codeparty/derby/issues/381