Golang:html/template库的自动空间/新线修剪

发布于 2025-02-11 17:24:43 字数 1658 浏览 1 评论 0原文

因此,我正在构建我的第一个Web应用程序,它基本上只是对ASCII-ART转换器的词。我首先通过标准HTML/模板库简单地显示我的ASCII-ART结果到HTML页面。但是,我遇到了一个问题,即我的字符串未按预期显示,当将其转换为HTML模板时,所有双层空格都会跳过。我知道您应该在HTML模板中放置一个负号和空格,以便打开空格修剪,但是即使没有它,也可以修剪空格。我认为我可能缺乏对该图书馆的基本理解,但是阅读文档并没有带任何地方,因此,如果您至少可以指导我朝为什么会发生的情况下指导我,我真的很感谢您的帮助。

这是它应该产生的以及它在终端中产生的东西

//i.sstatic.net/pefsd.png“ rel =“ nofollow noreferrer”>这是它在浏览器中产生的,起初我认为问题是在字体上,但是问题在这里,因为这里的问题是因为自动构成空间的trimmation trimming 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)
}

这是我的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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文