dragonsun00000 2019-06-21 09:32
浏览 56
已采纳

golang renderer.HTML无法从模板内部选择javascript文件

Trying to publish a login page using "github.com/thedevsaddam/renderer" package renderer . Not able to call the .js file from inside the template. When tried inlining javascript it worked fine, but not able to load the .js file.

my file structure is

Project
|
+-main.go
|
+-handlers
| |
| +- routes.go
| |
| +- login.go
+-views
| |
| +- _login.html
| +- login.js

main.go

package main

import (
    "fmt"
    "log"
    "net/http"

    "github.com/gorilla/mux"
    "github.com/higuestssg/handlers"
)

func main() {
    router := mux.NewRouter().StrictSlash(true)

    // This will serve files under http://localhost:8000/static/<filename>
    router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("views/"))))

    portalRouter := router.PathPrefix("/portal").Subrouter()
    handlers.HandleRoutes(portalRouter)
    fmt.Println("listening at localhost:10001")
    log.Fatal(http.ListenAndServe(":10001", router))
}

routes.go

package handlers

import (
  //"net/http"

  "github.com/gorilla/mux"
)

func HandleRoutes(r *mux.Router){
  r.HandleFunc("/login", loginHandler)
  r.HandleFunc("/healthTest", healthTestHandler)
}

login.go

package handlers

import (
    "fmt"
    "net/http"

    //"github.com/gorilla/mux"
    "github.com/thedevsaddam/renderer"
)

var rnd *renderer.Render

func init() {
    opts := renderer.Options{
        ParseGlobPattern: "./views/*.html",
    }
    rnd = renderer.New(opts)
}

// loginHandler renders login page
func loginHandler(w http.ResponseWriter, r *http.Request) {

    data := struct {
        Val1 string
        Val2 string
    }{
        "test100",
        "test2",
    }
    fmt.Println("login page hit")
    rnd.HTML(w, http.StatusOK, "_login", data)
}

_login.html

{{ define "_login" }}
<!--
https://medium.com/@thedevsaddam/easy-way-to-render-html-in-go-34575f858026
-->
<!DOCTYPE html>
<html lang="en">
<!-- Bootstrap import CSS-->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <body>
    <div class="container">
      <div class="starter-template jumbotron text-center">
        <h1>HI GUEST</h1>
        <div class="col-sm-4">
        <p class="lead">Welcome to <strong>HI_GUEST --ssg</strong> page</p>
        </div>
      </div>
    </div><!-- /.container -->
    <div class="row">
      <div class="col-sm-4"><p class="lead" id="idtest1">Test1</p></div>
      <div class="col-sm-4"><p class="lead">Test2</p></div>
      <div class="col-sm-4"><p class="lead"><button type="button" class="btn btn-info btn-lg" name="btn_ip" id="btn_id" onclick="myFunction()">{{.Val1}}</button></p></div>
    </div>
  </body>
  <script type='text/javascript' src='login.js'></script>
</html>
{{ end }}

login.js


function myFunction() 
{
 alert("Hello");
}

$(document).ready(function () {
  alert("Hello");
});

When checked in chrome using viewSource, when clicked on login.js its showing 404 not found

  • 写回答

1条回答 默认 最新

  • dr897777 2019-06-21 10:15
    关注

    updated _login.html , its working fine with @mkopriva suggestion

    {{ define "_login" }}
    <!--
    https://medium.com/@thedevsaddam/easy-way-to-render-html-in-go-34575f858026
    -->
    <!DOCTYPE html>
    <html lang="en">
    <!-- Bootstrap import CSS-->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
      <body>
        <div class="container">
          <div class="starter-template jumbotron text-center">
            <h1>HI GUEST</h1>
            <div class="col-sm-4">
            <p class="lead">Welcome to <strong>HI_GUEST --ssg</strong> page</p>
            </div>
          </div>
        </div><!-- /.container -->
        <div class="row">
          <div class="col-sm-4"><p class="lead" id="idtest1">Test1</p></div>
          <div class="col-sm-4"><p class="lead">Test2</p></div>
          <div class="col-sm-4"><p class="lead"><button type="button" class="btn btn-info btn-lg" name="btn_ip" id="btn_id" onclick="myFunction()">{{.Val1}}</button></p></div>
        </div>
      </body>
      <script type='text/javascript' src='/static/login.js'></script>
    </html>
    {{ end }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!