Template rendering with html/template and strict escaping

6267
0

Even in API-heavy systems, I occasionally render HTML emails or a lightweight admin page. I always use html/template (not text/template) so content is escaped by default, which prevents accidental XSS when variables contain user input. I also keep templates parsed at startup so errors surface during boot, not under traffic. The helper below renders to a bytes.Buffer first, then writes a complete response, which avoids partial output if execution fails. Another operational detail: templates should be treated like code and tested with representative data; a missing field can cause a runtime error if you’re not careful. For emails, I render both a plain-text and HTML variant and keep subject lines separate. This is a safe, boring approach that avoids the “string concatenation HTML” trap.