fix 'this' wrong usage (when var 'template' not created), make loop indexes local; create test page; tested in http://jsfiddle.net/alexbaumgertner/UXZDy/91/