[security] Fixing code generation for invalid templates.

This pull request fixes code generation for invalid template strings, which may result in execution of arbitrary Python code when rendering certain templates.