0

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

This is what it is producing in the browser, at first I thought the problem is with the font, however the problem is here because of automatic whitespace trimming

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>
Soncii
  • 11
  • Have you tried with `white-space: pre` CSS style? `

    `

    – icza Jun 30 '22 at 12:16
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jul 01 '22 at 09:08

0 Answers0