英文:
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.
"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?
答案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 (
	"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.
答案2
得分: 2
要在浏览器中打印此内容,请将\n替换为例如<br>,例如body = strings.Replace(body, "\n", "<br>", -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", "<br>", -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. <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: https://stackoverflow.com/questions/13779027/html-templates-replacing-newlines-with-br
I hope this helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论