Correctly catch template errors without returning invalid data

Fixes #6329
Merge pull request #6332 from jimi-c/issue_6329_space_in_var

Correctly catch template errors without returning invalid data