Golang GAE - HTML template that does not insert links to a web page

I am using Google Go on Google App Engine. I save the string description in a structure in a datastore , for example:

 type Foo struct{ Bar string } 

This description includes html tags, for example:

 <a href="/">Bar</a> 

I want the html template include this description in the html file so that it is parsed as html. For instance:

 <html><head><title>Title</title></head> <body>{{.Bar}}</body></html> 

for analysis:

 <html><head><title>Title</title></head> <body><a href="/">Bar</a></body></html> 

but instead, I get something like this:

 <html><head><title>Title</title></head> <body>&lt;a href=&#34;/&#34;&gt;Bar&#39;s&lt;/a&gt;</body></html> 

How can I make template correctly parse string into html link?

+4
source share
1 answer

Package "http/template" automatically deletes all lines. To get around this, you must make a value of type template.HTML . For instance.

 import "html/template" type Foo struct { Bar template.HTML } 

And then in your code do something like:

 Foo.Bar = template.HTML(barString) 
+5
source

Source: https://habr.com/ru/post/1443877/


All Articles