Improve behaviour with error_on_undefined_vars

* pass fail_on_undefined flag on recursive calls to template function,
   so more undefined variables are catched

 * raise correct exception from template_from_string function