Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
调用eq时出错:Go模板中用于比较的类型无效_Go_Go Templates - Fatal编程技术网

调用eq时出错:Go模板中用于比较的类型无效

调用eq时出错:Go模板中用于比较的类型无效,go,go-templates,Go,Go Templates,背景:一些静态网页共享相同的标题。我将通过判断活动标题项的状态(在Go模板中),用 _header.html <header class="row"> <div class="columns small-2"> <div class="logo-container"> <div class="logo"></div> <h1>Lemonade Stand

背景:一些静态网页共享相同的标题。我将通过判断活动标题项的状态(在Go模板中),用

_header.html

<header class="row">
    <div class="columns small-2">
        <div class="logo-container">
            <div class="logo"></div>
            <h1>Lemonade Stand Supply</h1>
        </div>
    </div>
    <div class="columns small-10">
        <div class="row">
            <div class="columns small-1 small-offset-9">
                <a class="secondary-color tiny" href="/login"> login </a>
            </div>
            <div class="columns small-2">
                <img src="/img/cart_small.png" alt="cart" />
            </div>
        </div>
        <div class="row">
            <nav class="columns small-6 small-offset-6">
                <div class="row">
                    <div class="columns small-3 item">
                        <a href="/home" class="{{ if eq .Active "home"}} active {{end}}">Home</a>
                    </div>
                    <div class="columns small-3 item">
                        <a href="/shop" class="{{ if eq .Active "shop"}} active {{end}}">Shop</a>
                    </div>
                    <div class="columns small-6 item">
                        <a href="." class="{{ if eq .Active "standlocator"}} active {{end}}">Stand Locator</a>
                    </div>
                </div>
            </nav>
        </div>
    </div>
</header>
查看模型-home.go

package viewmodel

type Home struct {
    Title  string
    Active string
}

func NewHome() Home {
    result := Home{
        Active: "home",
        Title:  "Lemonade Stand Supply",
    }
    return result
}
视图模型-shop.go

package viewmodel

type Shop struct {
    Title      string
    Active     string
    Categories []Category
}

type Category struct {
    URL         string
    ImageURL    string
    Title       string
    Description string
}

func NewShop() Shop {
    result := Shop{
        Title:  "Lemonade Stand Supply - Shop",
        Active: "shop",
    }
    juiceCategory := Category{
        URL:      "/shop_details",
        ImageURL: "lemon.png",
        Title:    "Juices and Mixes",
        Description: `Explore our wide assortment of juices and mixes expected by today's
        lemonade stand clientelle. Now featuring a full line of organic juices that are guaranceed
        to be obtained from trees that have never been treated with pesticide or artificial fertilizers.`,
    }
    supplyCategory := Category{
        URL:      ".",
        ImageURL: "kiwi.png",
        Title:    "Cups, Straws, and Other Supplies",
        Description: `From paper cups to bio-degradable plastic to straws and napkins,
        LSS is your source for the sundries that keep you stand running smoothl.`,
    }
    advertiseCategory := Category{
        URL:      ".",
        ImageURL: "pineapple.png",
        Title:    "Signs and Advertising",
        Description: `Sure, you could just wait for people to find your stand along
        the side of the road, but if you want to take it to the next level, our premium
        line of advertising supplies.`,
    }
    result.Categories = []Category{juiceCategory, supplyCategory, advertiseCategory}
    return result
}
这是本课程(模块5.7,)的后续内容。仔细地将每个文件与讲师提供的示例代码进行了匹配(该示例代码工作得非常好),我发现代码是相同的,仍然无法找到问题的原因。

将您的更改为此


{{.Title}
{{块“样式”{}{{end}
{{template“_header.html”。}
{{模板“内容”}
{{模板{u footer.html.}}
{{block“scripts”{}{{end}

您的基本模板不正确,这就是为什么会出现此错误。

这看起来很好。你的
模板中的问题。你救了我一天!我在“_layout.html”上漏掉了两个点,它们都在{{}表达式中。非常感谢!欢迎如果答案正确,请接受此答案。非常感谢。
<header class="row">
    <div class="columns small-2">
        <div class="logo-container">
            <div class="logo"></div>
            <h1>Lemonade Stand Supply</h1>
        </div>
    </div>
    <div class="columns small-10">
        <div class="row">
            <div class="columns small-1 small-offset-9">
                <a class="secondary-color tiny" href="/login"> login </a>
            </div>
            <div class="columns small-2">
                <img src="/img/cart_small.png" alt="cart" />
            </div>
        </div>
        <div class="row">
            <nav class="columns small-6 small-offset-6">
                <div class="row">
                    <div class="columns small-3 item">
                        <a href="/home" class="{{ if eq .Active "home"}} active {{end}}">Home</a>
                    </div>
                    <div class="columns small-3 item">
                        <a href="/shop" class="{{ if eq .Active "shop"}} active {{end}}">Shop</a>
                    </div>
                    <div class="columns small-6 item">
                        <a href="." class="{{ if eq .Active "standlocator"}} active {{end}}">Stand Locator</a>
                    </div>
                </div>
            </nav>
        </div>
    </div>
</header>
package main

import (
    "html/template"
    "io/ioutil"
    "log"
    "net/http"
    "os"

    "TutorialCreatingWebApplicationsWithGo/src/github.com/lss/webapp/viewmodel"
)

func main() {
    templates := populateTemplates()
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        requestedFile := r.URL.Path[1:]
        template := templates[requestedFile+".html"]
        var context interface{}
        switch requestedFile {
        case "shop":
            context = viewmodel.NewShop()
        default:
            context = viewmodel.NewHome()
        }
        if template != nil {
            err := template.Execute(w, context)
            if err != nil {
                log.Println(err)
            }
        } else {
            w.WriteHeader(http.StatusNotFound)
        }
    })
    http.Handle("/img/", http.FileServer(http.Dir("../../../../public")))
    http.Handle("/css/", http.FileServer(http.Dir("../../../../public")))
    http.ListenAndServe(":8080", nil)
}

func populateTemplates() map[string]*template.Template {
    result := make(map[string]*template.Template)
    const basePath = "../../../../templates"
    layout := template.Must(template.ParseFiles(basePath + "/_layout.html"))
    template.Must(layout.ParseFiles(basePath+"/_header.html", basePath+"/_footer.html"))
    dir, err := os.Open(basePath + "/content")
    if err != nil {
        panic("Failed to open template blocks directory: " + err.Error())
    }
    fis, err := dir.Readdir(-1)
    if err != nil {
        panic("Failed to read contents of content directory: " + err.Error())
    }
    for _, fi := range fis {
        f, err := os.Open(basePath + "/content/" + fi.Name())
        if err != nil {
            panic("Failed to open template '" + fi.Name() + "'")
        }
        content, err := ioutil.ReadAll(f)
        if err != nil {
            panic("Failed to read content from file '" + fi.Name() + "'")
        }
        f.Close()
        tmpl := template.Must(layout.Clone())
        _, err = tmpl.Parse(string(content))
        if err != nil {
            panic("Failed to parse contents of '" + fi.Name() + "' as template")
        }
        result[fi.Name()] = tmpl
    }
    return result
}
package viewmodel

type Home struct {
    Title  string
    Active string
}

func NewHome() Home {
    result := Home{
        Active: "home",
        Title:  "Lemonade Stand Supply",
    }
    return result
}
package viewmodel

type Shop struct {
    Title      string
    Active     string
    Categories []Category
}

type Category struct {
    URL         string
    ImageURL    string
    Title       string
    Description string
}

func NewShop() Shop {
    result := Shop{
        Title:  "Lemonade Stand Supply - Shop",
        Active: "shop",
    }
    juiceCategory := Category{
        URL:      "/shop_details",
        ImageURL: "lemon.png",
        Title:    "Juices and Mixes",
        Description: `Explore our wide assortment of juices and mixes expected by today's
        lemonade stand clientelle. Now featuring a full line of organic juices that are guaranceed
        to be obtained from trees that have never been treated with pesticide or artificial fertilizers.`,
    }
    supplyCategory := Category{
        URL:      ".",
        ImageURL: "kiwi.png",
        Title:    "Cups, Straws, and Other Supplies",
        Description: `From paper cups to bio-degradable plastic to straws and napkins,
        LSS is your source for the sundries that keep you stand running smoothl.`,
    }
    advertiseCategory := Category{
        URL:      ".",
        ImageURL: "pineapple.png",
        Title:    "Signs and Advertising",
        Description: `Sure, you could just wait for people to find your stand along
        the side of the road, but if you want to take it to the next level, our premium
        line of advertising supplies.`,
    }
    result.Categories = []Category{juiceCategory, supplyCategory, advertiseCategory}
    return result
}
<!DOCTYPE html>
<html>
<head>
<title>{{.Title}}</title>
<link href="/css/app.css" rel="stylesheet" />
{{block "styles" .}}{{end}}
</head>
<body>
    {{template "_header.html" .}}
    {{template "content" .}}
    {{template "_footer.html" .}}
    {{block "scripts" .}}{{end}}
</body>
</html>