HUGO
Menu
GitHub 90265 stars Mastodon

transform.HTMLEscape

Returns the given string, escaping special characters by replacing them with HTML entities.
Syntax
transform.HTMLEscape INPUT
Returns
string
Alias
htmlEscape

Usage

The transform.HTMLEscape function returns the given string, escaping special characters by replacing them with HTML entities. It escapes these five characters:

  • & → &
  • < → &lt;
  • > → &gt;
  • ' → &#39;
  • " → &#34;

When you print the result in an HTML template, Hugo escapes the entities again. To prevent this, pass the result through the safe.HTML function. See safe types for details.

Examples

{{ transform.HTMLEscape "Lilo & Stitch" }} → Lilo &amp; Stitch
{{ transform.HTMLEscape "7 > 6" }} → 7 &gt; 6

Comparing the result with and without safe.HTML, showing the raw HTML output:

{{ transform.HTMLEscape "Lilo & Stitch" }} → Lilo &amp;amp; Stitch
{{ transform.HTMLEscape "Lilo & Stitch" | safeHTML }} → Lilo &amp; Stitch