html - Golang HTML Web Apps 中没有这样的模板“xxx”
问题描述
我正在学习如何在 Go 中嵌入 HTML。然后我在运行 server.go 时收到此消息
模板执行错误:html/template:base.html:30:25: no such template "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 }}
我正确地编写了侧边栏模板吗?
任何帮助将非常感激。谢谢。
解决方案
不是 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"}
推荐阅读
- android - DialogFragments 出现在 OnResume()
- python-3.x - 如何通过部分字符串匹配搜索列名并通过字典替换列中的值?
- php - 使用 WHERE 函数选择多行
- file - UWP - 保存文件。IOException:该进程无法访问文件“文件路径”,因为它正被另一个进程使用
- database-design - KVS 如果是键,ID 应该在文档中吗?
- python - 解码以下字符串以获取 URL
- vhdl - VHDL 中的结构建模
- r - 当我尝试根据另一个数据框的列中的值对我的数据框进行子集化时出错
- javascript - Asp.net 谷歌地图标记
- node.js - 我收到一个参考错误,“在初始化之前无法访问‘成员’”