2017-01-19 16 views
0

ウェブルートディレクトリに 'views'というサブフォルダを作成しました。 Viewフォルダ内には、cssファイルとjsファイルを含む静的フォルダがあります。 Webルートにhtmlファイルがあると、htmlページがレンダリングされます。ただし、ビューフォルダ内に置かれてもレンダリングされません。私はtemplate.ParseGlobを使ってファイルを解析し、ExecuteTemplateを使ってレンダリングしています。Golangテンプレートがhtmlをレンダリングしない

package main 

import (
    "html/template" 
    "net/http" 

    "github.com/gorilla/mux" 
) 

var router = mux.NewRouter() 

var tmpl *template.Template 

func init() { 
    tmpl = template.Must(template.ParseGlob("view/*.html")) 
} 
func indexPage(w http.ResponseWriter, r *http.Request) { 
    err := tmpl.ExecuteTemplate(w, "signin", nil) 
    if err != nil { 
     http.Error(w, err.Error(), http.StatusInternalServerError) 
    } 
} 

func main() { 
    http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) 
    router.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("view/")))) 
    router.HandleFunc("/", indexPage) 
    http.ListenAndServe(":8091", router) 
} 

HTMLファイルが:

{{define "signin"}} 
{{template "header" .}} 
<h1 class="alert alert-info">Login</h1> 
<div class="container"> 
    {{with .Errors.message}} 
    <div class="alert alert-danger"> 
     {{.}} 
    </div> 
    {{end}} 
    <form method="POST" action="/"> 
     <label class="form-control" for="uname">User Name</label> 
     <input class="form-control" type="text" id="uname" name="uname"> 
     <label class="form-control" for="password">Password</label> 
     <input class="form-control" type="password" id="password" name="password"> 
     <button class="btn btn-info" type="submit">Submit</button> 
    </form> 

{{template "footer" .}} 
{{end}} 

はなぜ:私はsignin.htmlファイルに

{{ define "header"}} 
<html> 
<head> 
<meta charset="utf-8"> 
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<title>User Sign in</title> 
<meta name="description" content=""> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 

<link rel="stylesheet" href="/static/css/bootstrap.min.css"> 
<link rel="stylesheet" href="/static/css/bootstrap-theme.min.css"> 
<link rel="stylesheet" href="/static/css/main.css"> 

<script src="/static/js/vendor/modernizr-2.8.3-respond-1.4.2.min.js"></script> 
</head> 
<body> 
{{end}} 

{{define "footer"}} 
<footer class="panel-footer"><p>&copy; Company 2016</p></footer> 

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.11.2.min.js"><\/script>')</script> 

<script src="/static/js/vendor/bootstrap.min.js"></script> 

<script src="/static/js/main.js"></script> 

<!-- Google Analytics: change UA-XXXXX-X to be your site's ID. --> 
<script> 
      (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]= 
      function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date; 
      e=o.createElement(i);r=o.getElementsByTagName(i)[0]; 
      e.src='//www.google-analytics.com/analytics.js'; 
      r.parentNode.insertBefore(e,r)}(window,document,'script','ga')); 
      ga('create','UA-XXXXX-X','auto');ga('send','pageview'); 
</script> 
</body> 
</html> 
{{end}} 

signin.htmlファイルをrefernce index.htmlをにヘッダーとフッターを定義していますhtmlファイルをサブディレクトリ 'views'に配置すると、これは機能しません。変更されるのはparseGlobの引数だけです。私にとって

router.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("view/")))) 

作品:私はあなたがする必要があるすべて信じる

+1

フォルダ構造をツリーなどで共有できますか? –

答えて

0

は、この行を削除しています。あなたもあなたのHTMLをきれいにする必要があります - 私は少なくとも不足</div>を参照してください。

テンプレートはサーバー上で処理されるため、インターネット経由で提供する必要はありません。同時に、このルートエントリは、同じルートエントリ( "/")の別のハンドラを定義する次のエントリ(indexPage)と競合します。したがって、ブラウザで開くと、サーバはインターネット経由でテンプレートファイルを送信します。ディレクトリハンドラが最初にマッチするので、indexPageハンドラは呼び出されません。

「ビュー」フォルダについても言いますが、コードでは「ビュー」(末尾に「s」はありません)と書かれています。もう一つの単純な理由かもしれない。

関連する問題