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 -->
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改