dsdxlibt98300 2018-12-19 00:52
浏览 42
已采纳

在tmpl文件中使用引导程序显示导航栏

I am trying to get a simple nav bar working with GO.

Go Server:

func main() {
    tmpl := template.Must(template.New("navigator.tmpl").ParseFiles("./templates/navigator.tmpl"))

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    data := Names{
        PageTitle: "Holy Moly",
    }


    tmpl.Execute(w, data)
})
if err := http.ListenAndServe(":8080", nil); err != nil {
    panic(err)
}
}

This is my template file navigator.tmpl

{{ define "navigator" }}<!DOCTYPE html> {{ end }}
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Dashboard Template for Bootstrap</title>
    <!-- Bootstrap core CSS -->
    <link href="/go/src/dashboard/assets/dist/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="dashboard.css" rel="stylesheet">
  </head>
  <body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>           
          </button>
          <a class="navbar-brand" href="#">{{.PageTitle}}</a>
        </div>
  </body>
</html>

My Directory structure is as follows

Go:

=>src

  =>dashboard

    => main.go

    => assets

       =>dist

         =>bootstrap

            =>dist

               =>css/bootstrap.min.css

I am not able to get any of the bootstrap graphics. I see the title name but dont see the rectangualr nav bar.

When I open the file in my browser directly I can see the nav bar image with the title.

I am not sure at this point why the bootstrap graphics are not rendered properly

  • 写回答

1条回答 默认 最新

  • doukang8949 2018-12-19 01:25
    关注

    I assume you haven't create any routes for the static assets. So create one.

    http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("assets"))))
    

    By adding above handler, every request with /static/ prefix will be served using static files inside assets folder.

    Next, just change the url of the static asset.

    <link href="/static/dist/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/dashboard.css" rel="stylesheet"> <!-- I'm not sure where do you put the `dashboard.css` file, so just adjust this one -->
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败