2019-01-09 05:57

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

package main

import (

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)
    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

<!DOCTYPE html>
<html lang="en">
        <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"/>
        <div class="wrapper">
            {{  template "Sidebar" . }}
            <div class="main-panel">
                {{  template "Header" . }}
                <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" . }}
                {{  template "Footer" . }}

        <!--   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() {
        } );

    <!--  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>

{{ define "Sidebar" }}
    <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

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

That I write the sidebar template correctly?

Any help would be much appreciated. Thanks.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dongsanhu4784 dongsanhu4784 2年前

    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"}
    点赞 评论 复制链接分享