我正在学习在Go中嵌入HTML的知识。然后我在运行服务器时收到此消息。
模板执行错误:html / template:base.html:30:25:没有这样的模板“ Sidebar”
这是我的代码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)
}
}
我制作了sidebar.html模板并将其包含在base.html中,就像我包含index.html一样。我将按照本教程Golang Web Apps进行学习。不只是侧边栏,我也不能包含页眉和页脚
<!--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 }}
我正确编写了侧边栏模板吗?
任何帮助将不胜感激。谢谢。
答案 0 :(得分:1)
不是HTML WEB专家,而是
您的切片未正确创建,并且没有模板(页脚,边栏和页眉)作为元素。 当我尝试打印tmpl_list时,我看到以下结果。
[templates/base.html templates/index.html]
,如果您按如下所示创建tmpl_list,则它应该可以工作。 (暂时只是一种解决方法) 您需要查看创建切片tmpl_list的部分。
tmpl_list := []string{"templates/base.html", "templates/about.html", "templates/footer.html", "templates/header.html", "templates/sidebar.html"}