如何执行 html 模板

How to execute the html template

在此代码中,我试图执行 showAbout() 函数中存在的 tmpl.ExecuteTemplate(res, "about.html", d)

handler.go文件中,我有两个函数。第一个是 showAbout(),第二个是 about()about() 函数检查授权并在成功授权后,它转到具有 if 语句的 showAbout() 函数。

如果数据库中的 about 字段为空,则应执行 aboutform.html,获取数据并将其插入数据库。

如果数据插入成功,则显示消息并返回 about.html 以在那里显示数据。

只有about.html没有执行。虽然给出了成功信息。

about.html

<section>
   <h1>About</h1>
   <hr>
   <p>{{.Aboutdata}}</p>
</section>

aboutform.html

<form action="/about" method="POST">
    <section>
        <label>Content</label>
        <textarea name="content"></textarea>
        <div>
            <input type="submit" value="Submit">
        </div>
    </section>
</form>

db.go

func Insertdata(key, value string) bool {
    collection := Connect.Database("webApp3").Collection("data")
    filter := bson.M{"email": Account.Email, "password": Account.Password}
    update := bson.M{
        "$set": bson.M{
            key: value,
        },
    }
    _, err := collection.UpdateOne(context.TODO(), filter, update)
    return err == nil
}

handler.go

func showAbout(res http.ResponseWriter, req *http.Request) {
    d := struct{ Aboutdata string }{Aboutdata: database.Account.About}
    if d.Aboutdata == "" {
        tmpl.ExecuteTemplate(res, "aboutform.html", nil)
        content := req.FormValue("content")
        inserted := database.Insertdata("about", content)
        if inserted == true {
            fmt.Println("About is successfully inserted")
            tmpl.ExecuteTemplate(res, "about.html", d)   // It is not executing the about.html file
        } else {
            fmt.Println("About is not inserted")
        }
    } else {
        tmpl.ExecuteTemplate(res, "about.html", d)    // Although this same file is executing here.
    }
}

func about(res http.ResponseWriter, req *http.Request) {
    session, _ := store.Get(req, "session-name")
    var authenticated interface{} = session.Values["authenticated"]
    if authenticated != nil {
        isAuthenticated := session.Values["authenticated"].(bool)
        if !isAuthenticated {
            tmpl.ExecuteTemplate(res, "login.html", nil)
            return
        }
        showAbout(res, req)
    } else {
        tmpl.ExecuteTemplate(res, "login.html", nil)
        return
    }
}

以下是您的 about 处理程序的示例。请记住,该示例只是对使用单个处理程序处理 GET 和 POST 请求的通用结构的说明。据我所知,您问题中的代码包含许多您仍需解决的其他逻辑错误。

请注意,呈现模板和处理数据输入的逻辑在 GET 和 POST HTTP 方法之间进行了拆分。一些路由器允许基于方法的处理程序注册,在这种情况下,您可以有两个单独的处理程序,一个用于 showAbout,另一个用于 createAbout 或其他。

考虑到 if-else 块的结构,在此示例中使用 return 语句是不必要的,但是,我确实包含了它们以明确说明, 通常,在你写入响应后,你应该 没有 任何其他响应编写代码:没有 http.Redirect,没有更多的 ExecuteTemplate 调用,等等

func handleAbout(w http.ResponseWriter, r *http.Request) {
    if r.Method == "GET" {
         if data_is_present {
             if err := t.ExecuteTemplate(w, "about.html", nil); err != nil {
                 fmt.Println(err)
             }
             return
         } else if data_is_NOT_present {
             if err := t.ExecuteTemplate(w, "aboutform.html", nil); err != nil {
                 fmt.Println(err)
             }
             return
         }
    } else if r.Method == "POST" {
        content := r.FormValue("content")
        inserted := database.Insertdata("about", content)
        if inserted == true {
            d := struct{ Aboutdata string }{Aboutdata: content}
            if err := t.ExecuteTemplate(w, "about.html", d); err != nil {
                fmt.Println(err)
            }
            return
        } else {
            fmt.Println("About is not inserted")
            return
        }
    }
}