How to print new lines in Golang template?

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

How to print new lines in Golang template?

问题

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

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

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

它应该打印如下:

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

这是我的模板代码。

  1. <tr>
  2. <td>TextBody</td>
  3. <td>{{.Data.Content}}</td>
  4. </tr>

我漏掉了什么吗?

英文:

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

  1. &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

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

Here is my template code.

  1. &lt;tr&gt;
  2. &lt;td&gt;TextBody&lt;/td&gt;
  3. &lt;td&gt;{{.Data.Content}}&lt;/td&gt;
  4. &lt;/tr&gt;

Am I missing something?

答案1

得分: 2

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

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. txt := "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.google.com"
  8. res := strings.Split(txt, "\n")
  9. for _, val := range res {
  10. fmt.Println(val)
  11. }
  12. }

输出将是:

  1. Hi!
  2. How are you?
  3. Here is the link you wanted:
  4. 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.

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;strings&quot;
  5. )
  6. func main() {
  7. txt := &quot;Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.google.com&quot;
  8. res := strings.Split(txt, &quot;\n&quot;)
  9. for _, val := range res {
  10. fmt.Println(val)
  11. }
  12. }

The output will be:

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

Example on Go Playground.

答案2

得分: 2

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

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "html/template"
  6. "log"
  7. "net/http"
  8. "strings"
  9. )
  10. func main() {
  11. http.HandleFunc("/", ServeHTTP)
  12. if err := http.ListenAndServe(":80", nil); err != nil {
  13. log.Fatal(err)
  14. }
  15. }
  16. func ServeHTTP(w http.ResponseWriter, r *http.Request) {
  17. html := `
  18. <!DOCTYPE html>
  19. <html>
  20. <body>
  21. <table style="width:100%">
  22. <tr>
  23. <th>Data</th>
  24. <th>Content</th>
  25. </tr>
  26. <tr>
  27. <td>{{.Data}}</td>
  28. <td>{{.Content}}</td>
  29. </tr>
  30. </table>
  31. </body>
  32. </html>
  33. `
  34. st := "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.google.com"
  35. data := DataContent{"data", st}
  36. buf := &bytes.Buffer{}
  37. t := template.Must(template.New("template1").Parse(html))
  38. if err := t.Execute(buf, data); err != nil {
  39. panic(err)
  40. }
  41. body := buf.String()
  42. body = strings.Replace(body, "\n", "&lt;br&gt;", -1)
  43. fmt.Fprint(w, body)
  44. }
  45. type DataContent struct {
  46. Data, Content string
  47. }

要查看输出,请运行此代码并在浏览器中打开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:

  1. package main
  2. import (
  3. &quot;bytes&quot;
  4. &quot;fmt&quot;
  5. &quot;html/template&quot;
  6. &quot;log&quot;
  7. &quot;net/http&quot;
  8. &quot;strings&quot;
  9. )
  10. func main() {
  11. http.HandleFunc(&quot;/&quot;, ServeHTTP)
  12. if err := http.ListenAndServe(&quot;:80&quot;, nil); err != nil {
  13. log.Fatal(err)
  14. }
  15. }
  16. func ServeHTTP(w http.ResponseWriter, r *http.Request) {
  17. html := `
  18. &lt;!DOCTYPE html&gt;
  19. &lt;html&gt;
  20. &lt;body&gt;
  21. &lt;table style=&quot;width:100%&quot;&gt;
  22. &lt;tr&gt;
  23. &lt;th&gt;Data&lt;/th&gt;
  24. &lt;th&gt;Content&lt;/th&gt;
  25. &lt;/tr&gt;
  26. &lt;tr&gt;
  27. &lt;td&gt;{{.Data}}&lt;/td&gt;
  28. &lt;td&gt;{{.Content}}&lt;/td&gt;
  29. &lt;/tr&gt;
  30. &lt;/table&gt;
  31. &lt;/body&gt;
  32. &lt;/html&gt;
  33. `
  34. st := &quot;Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.google.com&quot;
  35. data := DataContent{&quot;data&quot;, st}
  36. buf := &amp;bytes.Buffer{}
  37. t := template.Must(template.New(&quot;template1&quot;).Parse(html))
  38. if err := t.Execute(buf, data); err != nil {
  39. panic(err)
  40. }
  41. body := buf.String()
  42. body = strings.Replace(body, &quot;\n&quot;, &quot;&lt;br&gt;&quot;, -1)
  43. fmt.Fprint(w, body)
  44. }
  45. type DataContent struct {
  46. Data, Content string
  47. }

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:

确定