How to print new lines in Golang template? [duplicate]
Asked Answered
L

2

6

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

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

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.

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

Am I missing something?

Lieutenancy answered 6/8, 2016 at 12:26 Comment(0)
H
2

To print this in browser, replace \n with e.g. <br>
like body = strings.Replace(body, "\n", "<br>", -1)
See this working sample code:

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", "<br>", -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: html/templates - Replacing newlines with <br>

I hope this helps.

Headed answered 6/8, 2016 at 13:49 Comment(0)
A
2

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 (
    "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)
    }
}

The output will be:

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

Example on Go Playground.

Alcaic answered 6/8, 2016 at 13:7 Comment(1)
Thanks Simo, but i want to print this in browser. Is there any template helpers already available in go?Lieutenancy
H
2

To print this in browser, replace \n with e.g. <br>
like body = strings.Replace(body, "\n", "<br>", -1)
See this working sample code:

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", "<br>", -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: html/templates - Replacing newlines with <br>

I hope this helps.

Headed answered 6/8, 2016 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.