dpus81500574 2019-01-09 05:57
浏览 80
已采纳

Golang HTML Web Apps中没有这样的模板“ xxx”

I'm learning about embedding HTML in Go. Then I get this message when I run the server.go

template executing error: html/template:base.html:30:25: no such template "Sidebar"

Here's my code Go-HTML-Template

//server.go    
package main

import (
    "fmt"
    "html/template"
    "io"
    "log"
    "net/http"
    "time"
)

const STATIC_URL string = "/assets/"
const STATIC_ROOT string = "assets/"

type Context struct {
    Title  string
    Static string
}

func Home(w http.ResponseWriter, req *http.Request) {
    context := Context{Title: "Welcome!"}
    render(w, "index", context)
}

func About(w http.ResponseWriter, req *http.Request) {
    context := Context{Title: "About"}
    render(w, "about", context)
}

func render(w http.ResponseWriter, tmpl string, context Context) {
    context.Static = STATIC_URL
    tmpl_list := []string{"templates/base.html",
        fmt.Sprintf("templates/%s.html", tmpl)}
    t, err := template.ParseFiles(tmpl_list...)
    if err != nil {
        log.Print("template parsing error: ", err)
    }
    err = t.Execute(w, context)
    if err != nil {
        log.Print("template executing error: ", err)
    }
}

func StaticHandler(w http.ResponseWriter, req *http.Request) {
    static_file := req.URL.Path[len(STATIC_URL):]
    if len(static_file) != 0 {
        f, err := http.Dir(STATIC_ROOT).Open(static_file)
        if err == nil {
            content := io.ReadSeeker(f)
            http.ServeContent(w, req, static_file, time.Now(), content)
            return
        }
    }
    http.NotFound(w, req)
}

func main() {
    http.HandleFunc("/", Home)
    http.HandleFunc("/about/", About)
    http.HandleFunc(STATIC_URL, StaticHandler)
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

I've made the sidebar.html template and included it in base.html like I includes index.html. I follow this tutorial Golang Web Apps for this learning. Not only sidebar, I can't include header and footer too

<!--base.html-->
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>CELERATES UNIVERSITY</title>

        <!-- Favicon     -->
        <link rel="icon" type="image/png" href="{{ .Static }}img/favicon2.ico">

        <!-- Bootstrap core CSS     -->
        <link rel="stylesheet" href="{{ .Static }}css/bootstrap.min.css" type="text/css">

        <!-- Animation library for notifications   -->
        <link href="{{ .Static }}css/animate.min.css" rel="stylesheet"/>

        <!--  Light Bootstrap Table core CSS    -->
        <link href="{{ .Static }}css/light-bootstrap-dashboard.css" rel="stylesheet"/>

        <!--     Fonts and icons     -->
        <link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
        <link href='http://fonts.googleapis.com/css?family=Roboto:400,700,300' rel='stylesheet' type='text/css'>
        <link href="{{ .Static }}css/pe-icon-7-stroke.css" rel="stylesheet" />

        <!--     Datatables     -->
        <link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"/>
    </head>
    <body>
        <div class="wrapper">
            <!--SIDEBAR-->
            {{  template "Sidebar" . }}
            <div class="main-panel">
                <!--HEADER-->
                {{  template "Header" . }}
                <!--CONTENT-->
                <div class="content">
                    <div class="container-fluid" align="center">
                        <div class="row">
                            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                            {{ template "content" . }}
                            </div>
                        </div>
                    </div>
                </div>
                <!--FOOTER-->
                {{  template "Footer" . }}
            </div>
        </div>


        <!--   JAVASCRIPTS   -->
        <!--   Core JS Files   -->
    <script src="https://code.jquery.com/jquery-3.3.1.js" type="text/javascript"></script>
    <script src="{{ .Static }}js/bootstrap.min.js" type="text/javascript"></script> 

    <!-- Datatables -->
    <script type="text/javascript" src="http://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script> 
    <script type="text/javascript">
        $(document).ready(function() {
            $("#example").DataTable();
        } );
    </script>

    <!--  Checkbox, Radio & Switch Plugins -->
    <script src="{{ .Static }}js/bootstrap-checkbox-radio-switch.js"></script>

    <!--  Charts Plugin -->
    <script src="{{ .Static }}js/chartist.min.js"></script>

    <!--  Notifications Plugin    -->
    <script src="{{ .Static }}js/bootstrap-notify.js"></script>

    <!--  Google Maps Plugin    -->
    <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>

    <!-- Light Bootstrap Table Core javascript and methods for Demo purpose -->
    <script src="{{ .Static }}js/light-bootstrap-dashboard.js"></script>
    </body>

{{ define "Sidebar" }}
<!--sidebar.html-->
    <div class="sidebar" data-color="blue" data-image="{{ .Static }}img/sidebar-4.jpg">
        <div class="sidebar-wrapper">
            <div class="logo">
                <a href="/" class="simple-text">
                    Celerates University
                </a>
            </div>

            <ul class="nav">
                <li>
                    <a href="/">
                        <i class="pe-7s-graph"></i>
                        <p>Dashboard</p>
                    </a>
                </li>
                <li>
                    <a href="/">
                        <i class="pe-7s-user"></i>
                        <p>Admin List</p>
                    </a>
                </li>
                <li>
                    <a href="/">
                        <i class="pe-7s-note2"></i>
                        <p>Student List</p>
                    </a>
                </li>
            </ul>
        </div>
    </div>
{{ end }}

That I write the sidebar template correctly?

Any help would be much appreciated. Thanks.

  • 写回答

1条回答 默认 最新

  • dongsanhu4784 2019-01-09 14:25
    关注

    Not an expert in HTML WEB but

    Your slice is not created properly and does not have templates(footer,sidebar and header) as elements. when i try to print tmpl_list, i see below result.

    [templates/base.html templates/index.html]
    

    and if you create tmpl_list as below, it should work. (just a workaround for the time being) You need to see the part where you are creating the slice tmpl_list.

    tmpl_list := []string{"templates/base.html", "templates/about.html", "templates/footer.html", "templates/header.html", "templates/sidebar.html"}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教