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>
`
– icza Jun 30 '22 at 12:16