gpt4 book ai didi

html - Golang HTML Web Apps 中没有这样的模板 "xxx"

转载 作者:数据小太阳 更新时间:2023-10-29 03:40:37 25 4
gpt4 key购买 nike

我正在学习如何在 Go 中嵌入 HTML。然后我在运行 server.go 时收到此消息

template executing error: 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 专家但是

您的 slice 创建不正确,没有模板(页脚、侧边栏和页眉)作为元素。当我尝试打印 tmpl_list 时,我看到以下结果。

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

如果您按如下方式创建 tmpl_list,它应该可以工作。 (暂时只是一个解决方法)您需要查看创建 slice tmpl_list 的部分。

tmpl_list := []string{"templates/base.html", "templates/about.html", "templates/footer.html", "templates/header.html", "templates/sidebar.html"}

关于html - Golang HTML Web Apps 中没有这样的模板 "xxx",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54104020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com