Golang:html/template库的自动空间/新线修剪
因此,我正在构建我的第一个Web应用程序,它基本上只是对ASCII-ART转换器的词。我首先通过标准HTML/模板库简单地显示我的ASCII-ART结果到HTML页面。但是,我遇到了一个问题,即我的字符串未按预期显示,当将其转换为HTML模板时,所有双层空格都会跳过。我知道您应该在HTML模板中放置一个负号和空格,以便打开空格修剪,但是即使没有它,也可以修剪空格。我认为我可能缺乏对该图书馆的基本理解,但是阅读文档并没有带任何地方,因此,如果您至少可以指导我朝为什么会发生的情况下指导我,我真的很感谢您的帮助。
这是它应该产生的以及它在终端中产生的东西
package main
import (
"fmt"
"html/template"
"net/http"
///"asci"
)
type WebData struct {
Title string
Text []string
}
func main() {
var Disp WebData
Disp.Title = "ASCII-ART WEB MY FIRST WEB APP"
Disp.Text = Conv("Hello, StackOverflow!")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl, _ := template.ParseFiles("index.html")
tmpl.Execute(w, Disp)
})
fmt.Printf("%s", Disp.Text)
fmt.Println("Server is listening...")
http.ListenAndServe(":8181", nil)
}
这是我的HTML模板:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ascii art web</title>
</head>
<body>
<h1 style="font-size: 25px;">Main page</h1>
<p style="color:blueviolet; font-family:monospace;" > {{range .Text}}{{.}}<br>{{end}}</p>
</body>
</html>
So, I'm building my first web app, it is basically just word to an ASCII-ART converter. I started with simple displaying of my ascii-art result to the html page by standard html/template library. However, I ran into a problem that my strings are not displayed as intended, when it is converted to html template all double whitespaces and newlines are skipped. I know that you should put a minus sign and whitespace in your html template in order to turn on whitespace trimming, however even without it, whitespaces are trimmed. I think I might be lacking of basic understanding of this library, but reading documentation hasn't brought anywhere, so if you could at least guide me in right direction of why it might be happening, I would really much appreciate your help.
Here is what it should produce and what it is producing in terminal
package main
import (
"fmt"
"html/template"
"net/http"
///"asci"
)
type WebData struct {
Title string
Text []string
}
func main() {
var Disp WebData
Disp.Title = "ASCII-ART WEB MY FIRST WEB APP"
Disp.Text = Conv("Hello, StackOverflow!")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl, _ := template.ParseFiles("index.html")
tmpl.Execute(w, Disp)
})
fmt.Printf("%s", Disp.Text)
fmt.Println("Server is listening...")
http.ListenAndServe(":8181", nil)
}
This is my html template:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ascii art web</title>
</head>
<body>
<h1 style="font-size: 25px;">Main page</h1>
<p style="color:blueviolet; font-family:monospace;" > {{range .Text}}{{.}}<br>{{end}}</p>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论