How to print new lines in Golang template?

huangapple go评论98阅读模式
英文:

How to print new lines in Golang template?

问题

我在MySQL中存储了一些内容,看起来像这样。

"Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.google.com"

当我在Golang模板中打印它时,它没有正确解析。我的意思是所有内容都显示在一行上。

它应该打印如下:

Hi!
How are you?
Here is the link you wanted:
http://www.google.com

这是我的模板代码。

<tr>
    <td>TextBody</td>
    <td>{{.Data.Content}}</td>
</tr>

我漏掉了什么吗?

英文:

I have stored some content in MySQL that looks like this.

&quot;Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.google.com&quot;

When I print it in Golang template, its not parsing correctly. I mean everything displayed in one line.

Its supposed to print like this

Hi!
How are you?
Here is the link you wanted:
http://www.google.com

Here is my template code.

&lt;tr&gt;
    &lt;td&gt;TextBody&lt;/td&gt;
    &lt;td&gt;{{.Data.Content}}&lt;/td&gt;
&lt;/tr&gt;

Am I missing something?

答案1

得分: 2

在这里,您可以使用Split函数来解析字符串并使用sep作为分隔符将子字符串拆分为片段。

package main

import (
	"fmt"
	"strings"
)

func main() {
	txt := "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.google.com"
	res := strings.Split(txt, "\n")
	for _, val := range res {
		fmt.Println(val)
	}
}

输出将是:

Hi!
How are you?
Here is the link you wanted:
http://www.google.com

Go Playground上的示例。

英文:

Here you can use the Split function to parse the string and split the substring into slices using the sep as separator.

package main

import (
	&quot;fmt&quot;
	&quot;strings&quot;
)

func main() {
	txt := &quot;Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.google.com&quot;
	res := strings.Split(txt, &quot;\n&quot;)
	for _, val := range res {
		fmt.Println(val)
	}
}

The output will be:

Hi!
How are you?
Here is the link you wanted:
http://www.google.com

Example on Go Playground.

答案2

得分: 2

要在浏览器中打印此内容,请将\n替换为例如&lt;br&gt;,例如body = strings.Replace(body, "\n", "&lt;br&gt;", -1)。请参阅以下示例代码:

package main

import (
	"bytes"
	"fmt"
	"html/template"
	"log"
	"net/http"
	"strings"
)

func main() {
	http.HandleFunc("/", ServeHTTP)
	if err := http.ListenAndServe(":80", nil); err != nil {
		log.Fatal(err)
	}
}

func ServeHTTP(w http.ResponseWriter, r *http.Request) {
	html := `
<!DOCTYPE html>
<html>
<body>  
<table style="width:100%">
  <tr>
    <th>Data</th>
    <th>Content</th> 
  </tr> 
  <tr>
    <td>{{.Data}}</td>
    <td>{{.Content}}</td>
  </tr>
</table> 
</body>
</html>
`
	st := "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.google.com"
	data := DataContent{"data", st}

	buf := &bytes.Buffer{}
	t := template.Must(template.New("template1").Parse(html))
	if err := t.Execute(buf, data); err != nil {
		panic(err)
	}
	body := buf.String()
	body = strings.Replace(body, "\n", "&lt;br&gt;", -1)
	fmt.Fprint(w, body)
}

type DataContent struct {
	Data, Content string
}

要查看输出,请运行此代码并在浏览器中打开http://127.0.0.1/

另请参阅:https://stackoverflow.com/questions/13779027/html-templates-replacing-newlines-with-br

希望对您有所帮助。

英文:

To print this in browser, replace \n with e.g. &lt;br&gt;
like body = strings.Replace(body, &quot;\n&quot;, &quot;&lt;br&gt;&quot;, -1)
See this working sample code:

package main

import (
	&quot;bytes&quot;
	&quot;fmt&quot;
	&quot;html/template&quot;
	&quot;log&quot;
	&quot;net/http&quot;
	&quot;strings&quot;
)

func main() {
	http.HandleFunc(&quot;/&quot;, ServeHTTP)
	if err := http.ListenAndServe(&quot;:80&quot;, nil); err != nil {
		log.Fatal(err)
	}
}

func ServeHTTP(w http.ResponseWriter, r *http.Request) {
	html := `
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;  
&lt;table style=&quot;width:100%&quot;&gt;
  &lt;tr&gt;
    &lt;th&gt;Data&lt;/th&gt;
    &lt;th&gt;Content&lt;/th&gt; 
  &lt;/tr&gt; 
  &lt;tr&gt;
    &lt;td&gt;{{.Data}}&lt;/td&gt;
    &lt;td&gt;{{.Content}}&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt; 
&lt;/body&gt;
&lt;/html&gt;
`
	st := &quot;Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.google.com&quot;
	data := DataContent{&quot;data&quot;, st}

	buf := &amp;bytes.Buffer{}
	t := template.Must(template.New(&quot;template1&quot;).Parse(html))
	if err := t.Execute(buf, data); err != nil {
		panic(err)
	}
	body := buf.String()
	body = strings.Replace(body, &quot;\n&quot;, &quot;&lt;br&gt;&quot;, -1)
	fmt.Fprint(w, body)
}

type DataContent struct {
	Data, Content string
}

To see the output, run this code and open your browser at http://127.0.0.1/

Also see: https://stackoverflow.com/questions/13779027/html-templates-replacing-newlines-with-br

I hope this helps.

huangapple
  • 本文由 发表于 2016年8月6日 20:26:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/38804290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定