unrolled/render

Unable to load assets via layout.tmpl from bindata

hachi8833 opened this issue · 4 comments

Hello, I'm stuck with an issue regarding the bindata.

I create a simplified project myapp for reproducing the issue.

Directory:

main.go
bindata.go
02_dist
├── scripts
│   └── main.8bd37191.js
├── styles
│   └── main.17e92ed5.css
└── template
│ └── index.tmpl
│ └── layout.tmpl

main.go:

package main

import (
    "flag"
    "net/http"

    ur "github.com/unrolled/render"
    "github.com/zenazn/goji"
)

//go:generate go-bindata 02_dist/...

type context struct {
    Title  string
    Result string
}

func main() {
    flag.Set("bind", ":3981")
    goji.DefaultMux.Get("/", index)
    goji.Serve()
}

func index(w http.ResponseWriter, r *http.Request) {
    ctx := context{
        Title:  "This is a title",
        Result: "This is a result",
    }

    re := setLayout()
    re.HTML(w, http.StatusOK, "index", ctx)
}

func setLayout() *ur.Render {
    return ur.New(ur.Options{
        Asset:      Asset,
        AssetNames: AssetNames,
        Directory:  "02_dist/template",
        Layout:     "layout",
    })
}

layout.tmpl:

<!DOCTYPE html>
<html lang='ja'>
  <head>
    <link rel="stylesheet" type="text/css" href="styles/main.17e92ed5.css">
    <title>{{ .Title }}</title>
  </head>
  <body>
    {{ yield }}
  </body>
  <script src="scripts/main.8bd37191.js"></script>
</html>

index.tmpl:

<div>{{ .Result }}</div>

(skipped css and js)

Run go run main.go bindata.go and accessing assets. All assets such as css, js, images or any other static files are unaccessible.

2015/11/29 17:58:51.119002 [MacBook-Pro.local/ZwzIfgQQvp-000002] Started GET "/styles/main.17e92ed5.css" from [::1]:64159
2015/11/29 17:58:51.119044 [MacBook-Pro.local/ZwzIfgQQvp-000002] Returning 404 in 13.154µs
2015/11/29 17:58:53.552403 [MacBook-Pro.local/ZwzIfgQQvp-000003] Started GET "/scripts/main.8bd37191.js" from [::1]:64159
2015/11/29 17:58:53.552437 [MacBook-Pro.local/ZwzIfgQQvp-000003] Returning 404 in 10.008µs

Please note that I'd like to convert all assets, including template files, into bindata.go.
Any other features work fine, except referring assets within bindata.go from the template.

Perhaps I might misunderstand or overlook something.
Could you please let me know how to resolve the references in bindata.go file?

Best regards,

Where are you serving your bindata assets? They need a route—such as
"/static"—to be mounted under.
On Sun, 29 Nov 2015 at 5:10 PM, hachi8833 notifications@github.com wrote:

Hello, I'm stuck with an issue regarding the bindata.

I create a simplified project myapp https://github.com/hachi8833/myapp
for reproducing the issue.

Directory:

main.go
bindata.go
02_dist
├── scripts
│ └── main.8bd37191.js
├── styles
│ └── main.17e92ed5.css
└── template
└── index.tmpl
└── layout.tmpl

main.go:

package main
import (
"flag"
"net/http"

ur "github.com/unrolled/render"
"github.com/zenazn/goji"

)
//go:generate go-bindata 02_dist/...
type context struct {
Title string
Result string
}
func main() {
flag.Set("bind", ":3981")
goji.DefaultMux.Get("/", index)
goji.Serve()
}
func index(w http.ResponseWriter, r *http.Request) {
ctx := context{
Title: "This is a title",
Result: "This is a result",
}

re := setLayout()
re.HTML(w, http.StatusOK, "index", ctx)

}
func setLayout() *ur.Render {
return ur.New(ur.Options{
Asset: Asset,
AssetNames: AssetNames,
Directory: "02_dist/template",
Layout: "layout",
})
}

layout.tmpl:

<title>{{ .Title }}</title> {{ yield }} <script src="scripts/main.8bd37191.js"></script>

index.tmpl:

{{ .Result }}

(skipped css and js)

Run go run main.go bindata.go and accessing assets. All assets such as
css, js, images or any other static files are unaccessible.

2015/11/29 17:58:51.119002 [MacBook-Pro.local/ZwzIfgQQvp-000002] Started
GET "/styles/main.17e92ed5.css" from [::1]:64159
2015/11/29 17:58:51.119044 [MacBook-Pro.local/ZwzIfgQQvp-000002] Returning
404 in 13.154µs
2015/11/29 17:58:53.552403 [MacBook-Pro.local/ZwzIfgQQvp-000003] Started
GET "/scripts/main.8bd37191.js" from [::1]:64159
2015/11/29 17:58:53.552437 [MacBook-Pro.local/ZwzIfgQQvp-000003] Returning
404 in 10.008µs

Please note that I'd like to convert all assets, including template files,
into bindata.go.
Any other features work fine, except referring assets within bindata.go
from the template.

Perhaps I might misunderstand or overlook something.
Could you please let me know how to resolve the references in bindata.go
file?

Best regards,


Reply to this email directly or view it on GitHub
#44.

Thank you very much! Perhaps that's what I overlooked.
I'm looking into it but I'm wondering how to add the route to bindata...

This project in the go-bindata README should help:
https://github.com/elazarl/go-bindata-assetfs#readme
On Sun, 29 Nov 2015 at 6:21 PM, hachi8833 notifications@github.com wrote:

Thank you very much! Perhaps that's what I overlooked.
I'm looking into it but I'm wondering how to add the route to bindata...


Reply to this email directly or view it on GitHub
#44 (comment).

Finally resolved the issue as you suggested https://github.com/hachi8833/myapp
Thank you so much for your kindness!