Home >Backend Development >Golang >How to Handle HTML and JSON Insertion in Go Templates Without Escaping Issues?
Handling HTML and JSON Insertion in Go Templates
Inserting HTML or JSON into Go templates can lead to escaping and other output formatting issues. To ensure the intended output, follow these guidelines:
Inserting HTML:
Use template.HTML instead of strings to prevent escaping. Example:
<code class="go">tplVars := map[string]interface{}{ "Html": template.HTML("<p>Paragraph</p>"), }</code>
Inserting JSON:
Pass JSON data as an interface{} value. Example:
<code class="go">type Data struct { Html string Json interface{} }</code>
In the template:
<code class="go">{{.Data.Html}} {{.Data.Json}}</code>
Additional Notes:
The above is the detailed content of How to Handle HTML and JSON Insertion in Go Templates Without Escaping Issues?. For more information, please follow other related articles on the PHP Chinese website!