简体   繁体   English

GoLang 中的 HTML 部分

[英]HTML Partials in GoLang

I am just getting started with Go, and I want to create a web app with it.我刚刚开始使用 Go,我想用它创建一个 Web 应用程序。 What I am trying now is to use the templating in a handlebarsjs-esque manner.我现在正在尝试以一种 handlebarsjs 风格的方式使用模板。 I want to pull my header and footer out of my main page so I can inject them on every webpage.我想把我的页眉和页脚从我的主页中拉出来,这样我就可以在每个网页上注入它们。

My current set up is supposed to parse the homepage, header, and footer HTML files and cache them.我当前的设置应该解析主页、页眉和页脚 HTML 文件并缓存它们。 Then I execute my home.html template, which has fields for the pages Title, the header.html file, and the footer.html file.然后我执行我的 home.html 模板,其中包含页面标题、header.html 文件和 footer.html 文件的字段。

Whenever I search for similar pages I just see javascript pages, so if this is a repost, let me know where to look.每当我搜索类似的页面时,我只会看到 javascript 页面,所以如果这是转帖,请告诉我在哪里查看。

edit: I have updated my code to take tips from the answers by @Minty and @putu.编辑:我已更新我的代码以从@Minty 和@putu 的答案中获取提示。 I am trying to read the html files and store them in a data map, while also adding the template define to my templates.我正在尝试读取 html 文件并将它们存储在数据映射中,同时还将模板define添加到我的模板中。 There are some new bugs that I am working on squashing, so the site does not render currently.我正在处理一些新的错误,因此该站点当前无法呈现。 But, if there are any new tips you can give, that would help a lot.但是,如果您可以提供任何新提示,那将大有帮助。

server.go server.go

package main

import (
    "html/template"
    "io/ioutil"
    "net/http"
    "regexp"
)

var tPath = "./temps/"
var dPath = "./data/"

var templates = template.Must(template.ParseFiles(tPath+"home.html", dPath+"header.html", dPath+"footer.html"))
var validPath = regexp.MustCompile("^/")

func rootHandler(wr http.ResponseWriter, req *http.Request) {
    title := "home"
    headerFile, headErr := ioutil.ReadFile(dPath + "header.html")
    footerFile, footErr := ioutil.ReadFile(dPath + "footer.html")

    if headErr != nil || footErr != nil {
        http.Error(wr, headErr.Error(), http.StatusInternalServerError)
        http.Error(wr, footErr.Error(), http.StatusInternalServerError)
    }

    data := map[string]interface{}{
        "Title":  title,
        "Header": string(headerFile),
        "Footer": string(footerFile),
    }

    err := templates.ExecuteTemplate(wr, title+".html", data)

    if err != nil {
        http.Error(wr, err.Error(), http.StatusInternalServerError)
    }
}

func main() {
    http.HandleFunc("/", rootHandler)
    http.ListenAndServe(":8080", nil)
}

home.html:主页.html:

{{define "homeHTML"}}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>{{.Title}} - MySite</title>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        {{.Header}}
        <h1>Welcome!</h1>
        {{.Footer}}
    </body>
</html>
{{end}}

Header.html:标题.html:

{{define "headerHTML"}}
<header>
    <h1>MySite</h1>
    <br>
    <nav>
        <a href="/">Home</a>
    </nav>
</header>
{{end}}

Footer.html页脚.html

{{define "footerHTML"}}
<footer>
    <p>Thank You for Visiting</p>
</footer>
{{end}}

This is full working example.这是完整的工作示例。

home.html inside temps folder: temps 文件夹中的 home.html:

{{define "homeHTML"}}

{{template "headHTML" .}}

{{template "headerHTML" .}}

{{template "footerHTML" .}}

{{end}}

head.html inside data folder:数据文件夹内的 head.html:

{{define "headHTML"}}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>{{.title}} - MySite</title>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>  
{{end}}

header.html inside data folder数据文件夹内的 header.html

{{define "headerHTML"}}
<header>
    {{.header}}
    <h1>Welcome to my site!</h1>
    <br>
    <nav>
        <a href="/">Home</a>
    </nav>
</header>
{{end}}

footer.html inside data folder:数据文件夹内的footer.html:

{{define "footerHTML"}}
<h1>Welcome! {{.footer}}</h1>
<footer>
<p>Thank You for Visiting</p>
</footer>
</body>
</html>
{{end}}

And the code will be like this代码将是这样的

package main

import (
    "html/template"
    "io/ioutil"
    "net/http"
    "path/filepath"
    "strings"
)

var tPath = "./temps/"
var dPath = "./data/"

var templateDirs = []string{"temps", "data"}
var templates *template.Template

func getTemplates() (templates *template.Template, err error) {
    var allFiles []string
    for _, dir := range templateDirs {
        files2, _ := ioutil.ReadDir(dir)
        for _, file := range files2 {
            filename := file.Name()
            if strings.HasSuffix(filename, ".html") {
                filePath := filepath.Join(dir, filename)
                allFiles = append(allFiles, filePath)
            }
        }
    }

    templates, err = template.New("").ParseFiles(allFiles...)
    return
}

func init() {
    templates, _ = getTemplates()
}

func rootHandler(wr http.ResponseWriter, req *http.Request) {
    title := "home"

    data := map[string]interface{}{
        "title":  title,
        "header": "My Header",
        "footer": "My Footer",
    }

    err := templates.ExecuteTemplate(wr, "homeHTML", data)

    if err != nil {
        http.Error(wr, err.Error(), http.StatusInternalServerError)
    }
}

func main() {
    http.HandleFunc("/", rootHandler)
    http.ListenAndServe(":8080", nil)
}

You need to pass the data as struct or map to template.您需要将数据作为structmap传递给模板。 Example of your rootHandler using map :使用maprootHandler示例:

func rootHandler(wr http.ResponseWriter, req *http.Request) {
    title := "home"
    //Wrap your variable into a map
    data := map[string]interface{}{
        "Title":  title,
        "IntVar": 100,
    }

    err := templates.ExecuteTemplate(wr, title+".html", data)

    //other codes...
}

For more details, see documentation .有关更多详细信息,请参阅文档

The accepted answer can be simplified by using template.ParseGlob可以使用 template.ParseGlob 简化已接受的答案

Replace the contents of func getTemplates() with将 func getTemplates() 的内容替换为

 templates, err:= template.ParseGlob("temps/*")
 if err!= nil {
   return nil, err
 }
 return templates.ParseGlob("data/*)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM