Fix invalid template loop & prevent Twig from throwing errors in invalid uses of BEGINELSE

http://tracker.phpbb.com/browse/PHPBB3-11833