mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 02:31:04 +01:00
add layouts to the _examples/view/embedding-templates-into-app
Former-commit-id: 15085969eb5cad90022b249782e0620d4f5d267a
This commit is contained in:
parent
6d94a51d3d
commit
a9ac0aee2f
|
@ -1,6 +1,9 @@
|
|||
// Code generated by go-bindata.
|
||||
// sources:
|
||||
// templates/hi.html
|
||||
// templates/layouts/layout.html
|
||||
// templates/layouts/mylayout.html
|
||||
// templates/page1.html
|
||||
// templates/partials/page1_partial1.html
|
||||
// DO NOT EDIT!
|
||||
|
||||
package main
|
||||
|
@ -68,22 +71,82 @@ func (fi bindataFileInfo) Sys() interface{} {
|
|||
return nil
|
||||
}
|
||||
|
||||
var _templatesHiHtml = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xb2\xc9\x28\xc9\xcd\xb1\xe3\xe5\xb2\xc9\x48\x4d\x4c\x01\xd1\x25\x99\x25\x39\xa9\x76\xd5\xd5\x7a\x21\x20\x46\x6d\xad\x8d\x3e\x44\x84\x97\xcb\x46\x1f\xa6\x26\x29\x3f\xa5\xd2\x8e\x97\x8b\xd3\x26\xc3\xd0\xce\x23\x53\xa1\xba\x5a\xcf\x2f\x31\x37\xb5\xb6\x16\xa4\x06\x2a\x67\xa3\x0f\x35\x18\x10\x00\x00\xff\xff\x61\x88\xba\x25\x61\x00\x00\x00")
|
||||
var _templatesLayoutsLayoutHtml = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x34\xce\xc1\xa9\xc3\x30\x0c\x06\xe0\xf3\x33\x78\x07\xbd\x01\x8c\xc9\x5d\x78\x82\x9e\x4a\x17\x70\x6a\x51\x19\x94\xa4\x38\xca\xc1\x84\xec\x5e\xec\xba\x27\x49\xf0\x89\xff\x47\xd6\x45\x82\x35\xc8\x14\x53\x9b\x9a\x55\x28\xdc\x62\xdd\x0e\x45\xff\xbd\xac\xb1\x06\xfd\x4f\xcc\x5b\xaa\xc1\x9a\x3f\xe4\x29\x3c\x38\xef\x90\x77\x50\x26\x78\xc9\x36\x47\x01\x19\xaf\x3c\x75\x34\x17\xf0\x7d\xf9\x77\x0e\xee\xb4\x26\x2a\x5d\x3f\x8f\x52\x68\x55\x50\x5a\xde\x12\x95\x80\xa9\x10\x38\xd7\xec\x79\x42\xcd\x24\x09\xae\xab\x05\x8f\x40\xf4\xa3\xeb\x27\x00\x00\xff\xff\x68\xca\x16\xc2\xb4\x00\x00\x00")
|
||||
|
||||
func templatesHiHtmlBytes() ([]byte, error) {
|
||||
func templatesLayoutsLayoutHtmlBytes() ([]byte, error) {
|
||||
return bindataRead(
|
||||
_templatesHiHtml,
|
||||
"templates/hi.html",
|
||||
_templatesLayoutsLayoutHtml,
|
||||
"templates/layouts/layout.html",
|
||||
)
|
||||
}
|
||||
|
||||
func templatesHiHtml() (*asset, error) {
|
||||
bytes, err := templatesHiHtmlBytes()
|
||||
func templatesLayoutsLayoutHtml() (*asset, error) {
|
||||
bytes, err := templatesLayoutsLayoutHtmlBytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := bindataFileInfo{name: "templates/hi.html", size: 97, mode: os.FileMode(438), modTime: time.Unix(1496244983, 0)}
|
||||
info := bindataFileInfo{name: "templates/layouts/layout.html", size: 180, mode: os.FileMode(438), modTime: time.Unix(1499700235, 0)}
|
||||
a := &asset{bytes: bytes, info: info}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
var _templatesLayoutsMylayoutHtml = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x34\x8f\x4d\x6a\xc5\x30\x0c\x84\xd7\x35\xf8\x0e\xd3\x03\x18\x93\xbd\xf1\x09\xba\x2a\xbd\x80\x53\xab\xc8\xe0\x9f\xe2\x28\x0b\x13\x72\xf7\x47\x9c\xbc\x95\x46\x62\x46\x7c\xe3\x58\x4a\xf6\x5a\x39\xa6\x10\xaf\x29\x49\x32\xf9\x32\xf0\x15\x46\xdb\xc5\xd9\xfb\xa0\x95\x56\xce\xbe\x4d\x6b\x8b\xc3\x6b\xf5\xe1\x78\xf1\x3f\x9c\x36\xa4\x0d\xc2\x84\x3c\x33\xf8\x6b\x7d\xae\xb6\x0c\x8b\x50\xe3\x14\x4d\x98\x3a\x7a\xdb\x85\x36\xb4\x9a\x87\xb3\xbc\xcc\x27\x6b\x87\x9d\xe2\xd3\x18\x7c\x53\x8d\x74\xc7\x7f\xf7\xde\xa9\x0a\x84\xca\x7f\x0e\x42\x60\xea\x04\x63\x2e\xef\x71\x60\x24\xca\x11\xe7\x79\x81\x3d\x40\xce\x3e\x75\x5e\x01\x00\x00\xff\xff\x64\xea\xc5\x1d\xd7\x00\x00\x00")
|
||||
|
||||
func templatesLayoutsMylayoutHtmlBytes() ([]byte, error) {
|
||||
return bindataRead(
|
||||
_templatesLayoutsMylayoutHtml,
|
||||
"templates/layouts/mylayout.html",
|
||||
)
|
||||
}
|
||||
|
||||
func templatesLayoutsMylayoutHtml() (*asset, error) {
|
||||
bytes, err := templatesLayoutsMylayoutHtmlBytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := bindataFileInfo{name: "templates/layouts/mylayout.html", size: 215, mode: os.FileMode(438), modTime: time.Unix(1499700235, 0)}
|
||||
a := &asset{bytes: bytes, info: info}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
var _templatesPage1Html = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x3c\xca\x41\xaa\xc2\x30\x10\x00\xd0\xf5\x2f\xf4\x0e\xc3\xec\xbf\x25\x5b\x8d\x3d\x83\x37\x90\x69\x33\xa4\xa1\x63\x53\x26\x69\x40\x42\xee\x2e\xa2\xb8\x7c\xf0\xac\x0b\x05\x52\x7e\x0a\x5f\x71\xa2\x79\xf5\x1a\x8f\xcd\xfd\xcf\x51\xa2\x9e\x61\x12\x9a\xd7\x0b\xfc\x74\x30\x8e\x7d\xd7\x77\x7f\x76\x31\xe3\x8d\x3c\x83\x81\x5a\xc1\x2b\x73\x06\x0c\x1a\x12\x38\x2e\x2c\x71\x67\xc5\xd6\xec\xb0\x98\xcf\xaf\x15\x94\x37\xc7\x0a\xb8\x93\xe6\x40\x92\x86\x9d\x3c\x9b\xfb\x97\xe6\xb4\xe4\x87\x60\x6b\xef\x6e\x07\x17\xca\xd8\x77\xaf\x00\x00\x00\xff\xff\x47\x41\x4a\x5c\x9d\x00\x00\x00")
|
||||
|
||||
func templatesPage1HtmlBytes() ([]byte, error) {
|
||||
return bindataRead(
|
||||
_templatesPage1Html,
|
||||
"templates/page1.html",
|
||||
)
|
||||
}
|
||||
|
||||
func templatesPage1Html() (*asset, error) {
|
||||
bytes, err := templatesPage1HtmlBytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := bindataFileInfo{name: "templates/page1.html", size: 157, mode: os.FileMode(438), modTime: time.Unix(1499700235, 0)}
|
||||
a := &asset{bytes: bytes, info: info}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
var _templatesPartialsPage1_partial1Html = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xb2\x49\xc9\x2c\x53\x28\x2e\xa9\xcc\x49\xb5\x55\x4a\x4a\x4c\xce\x4e\x2f\xca\x2f\xcd\x4b\xd1\x4d\xce\xcf\xc9\x2f\xb2\x52\x28\xcf\xc8\x2c\x49\xb5\x56\x80\xf2\x8a\x52\x53\x94\xec\x78\xb9\x38\x6d\x32\x0c\xed\x02\x12\xd3\x53\x15\x0c\xd5\x8b\x15\x02\x12\x8b\x4a\x32\x13\x73\x14\x0c\x6d\xf4\x33\x0c\xed\x78\xb9\x6c\xf4\x53\x32\xcb\xec\x78\xb9\x00\x01\x00\x00\xff\xff\xa2\xa6\x60\xb6\x59\x00\x00\x00")
|
||||
|
||||
func templatesPartialsPage1_partial1HtmlBytes() ([]byte, error) {
|
||||
return bindataRead(
|
||||
_templatesPartialsPage1_partial1Html,
|
||||
"templates/partials/page1_partial1.html",
|
||||
)
|
||||
}
|
||||
|
||||
func templatesPartialsPage1_partial1Html() (*asset, error) {
|
||||
bytes, err := templatesPartialsPage1_partial1HtmlBytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := bindataFileInfo{name: "templates/partials/page1_partial1.html", size: 89, mode: os.FileMode(438), modTime: time.Unix(1499700235, 0)}
|
||||
a := &asset{bytes: bytes, info: info}
|
||||
return a, nil
|
||||
}
|
||||
|
@ -140,7 +203,10 @@ func AssetNames() []string {
|
|||
|
||||
// _bindata is a table, holding each asset generator, mapped to its name.
|
||||
var _bindata = map[string]func() (*asset, error){
|
||||
"templates/hi.html": templatesHiHtml,
|
||||
"templates/layouts/layout.html": templatesLayoutsLayoutHtml,
|
||||
"templates/layouts/mylayout.html": templatesLayoutsMylayoutHtml,
|
||||
"templates/page1.html": templatesPage1Html,
|
||||
"templates/partials/page1_partial1.html": templatesPartialsPage1_partial1Html,
|
||||
}
|
||||
|
||||
// AssetDir returns the file names below a certain
|
||||
|
@ -182,10 +248,16 @@ type bintree struct {
|
|||
Func func() (*asset, error)
|
||||
Children map[string]*bintree
|
||||
}
|
||||
|
||||
var _bintree = &bintree{nil, map[string]*bintree{
|
||||
"templates": {nil, map[string]*bintree{
|
||||
"hi.html": {templatesHiHtml, map[string]*bintree{}},
|
||||
"templates": &bintree{nil, map[string]*bintree{
|
||||
"layouts": &bintree{nil, map[string]*bintree{
|
||||
"layout.html": &bintree{templatesLayoutsLayoutHtml, map[string]*bintree{}},
|
||||
"mylayout.html": &bintree{templatesLayoutsMylayoutHtml, map[string]*bintree{}},
|
||||
}},
|
||||
"page1.html": &bintree{templatesPage1Html, map[string]*bintree{}},
|
||||
"partials": &bintree{nil, map[string]*bintree{
|
||||
"page1_partial1.html": &bintree{templatesPartialsPage1_partial1Html, map[string]*bintree{}},
|
||||
}},
|
||||
}},
|
||||
}}
|
||||
|
||||
|
@ -235,3 +307,4 @@ func _filePath(dir, name string) string {
|
|||
cannonicalName := strings.Replace(name, "\\", "/", -1)
|
||||
return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...)
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
3c9dbd9acb1d553cc63940c72f212a880e8a8073
|
|
@ -6,23 +6,57 @@ import (
|
|||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
tmpl := iris.HTML("./templates", ".html")
|
||||
tmpl.Layout("layouts/layout.html")
|
||||
tmpl.AddFunc("greet", func(s string) string {
|
||||
return "Greetings " + s + "!"
|
||||
})
|
||||
|
||||
// $ go get -u github.com/jteeuwen/go-bindata/...
|
||||
// $ go-bindata ./templates/...
|
||||
// $ go build
|
||||
// $ ./embedding-templates-into-app
|
||||
// html files are not used, you can delete the folder and run the example
|
||||
app.RegisterView(iris.HTML("./templates", ".html").Binary(Asset, AssetNames))
|
||||
app.Get("/", hi)
|
||||
// html files are not used, you can delete the folder and run the example.
|
||||
tmpl.Binary(Asset, AssetNames) // <-- IMPORTANT
|
||||
|
||||
app.RegisterView(tmpl)
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
if err := ctx.View("page1.html"); err != nil {
|
||||
ctx.StatusCode(iris.StatusInternalServerError)
|
||||
ctx.Writef(err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
// remove the layout for a specific route
|
||||
app.Get("/nolayout", func(ctx iris.Context) {
|
||||
ctx.ViewLayout(iris.NoLayout)
|
||||
if err := ctx.View("page1.html"); err != nil {
|
||||
ctx.StatusCode(iris.StatusInternalServerError)
|
||||
ctx.Writef(err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
// set a layout for a party, .Layout should be BEFORE any Get or other Handle party's method
|
||||
my := app.Party("/my").Layout("layouts/mylayout.html")
|
||||
{ // both of these will use the layouts/mylayout.html as their layout.
|
||||
my.Get("/", func(ctx iris.Context) {
|
||||
ctx.View("page1.html")
|
||||
})
|
||||
my.Get("/other", func(ctx iris.Context) {
|
||||
ctx.View("page1.html")
|
||||
})
|
||||
}
|
||||
|
||||
// http://localhost:8080
|
||||
// http://localhost:8080/nolayout
|
||||
// http://localhost:8080/my
|
||||
// http://localhost:8080/my/other
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
type page struct {
|
||||
Title, Name string
|
||||
}
|
||||
|
||||
func hi(ctx iris.Context) {
|
||||
ctx.ViewData("", page{Title: "Hi Page", Name: "iris"})
|
||||
ctx.View("hi.html")
|
||||
}
|
||||
// Note for new Gophers:
|
||||
// `go build` is used instead of `go run main.go` as the example comments says
|
||||
// otherwise you will get compile errors, this is a Go thing;
|
||||
// because you have multiple files in the `package main`.
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
<html>
|
||||
|
||||
<head>
|
||||
<title>{{.Title}}</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Hi {{.Name}}
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -0,0 +1,12 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Layout</title>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<h1>This is the global layout</h1>
|
||||
<br />
|
||||
<!-- Render the current template here -->
|
||||
{{ yield }}
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,12 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>my Layout</title>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<h1>This is the layout for the /my/ and /my/other routes only</h1>
|
||||
<br />
|
||||
<!-- Render the current template here -->
|
||||
{{ yield }}
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,7 @@
|
|||
<div style="background-color: black; color: blue">
|
||||
|
||||
<h1>Page 1 {{ greet "iris developer"}}</h1>
|
||||
|
||||
{{ render "partials/page1_partial1.html"}}
|
||||
|
||||
</div>
|
|
@ -0,0 +1,3 @@
|
|||
<div style="background-color: white; color: red">
|
||||
<h1>Page 1's Partial 1</h1>
|
||||
</div>
|
72
view/html.go
72
view/html.go
|
@ -176,11 +176,34 @@ func (s *HTMLEngine) AddFunc(funcName string, funcBody interface{}) {
|
|||
}
|
||||
|
||||
// Load parses the templates to the engine.
|
||||
// It's alos responsible to add the necessary global functions.
|
||||
// It's also responsible to add the necessary global functions.
|
||||
//
|
||||
// Returns an error if something bad happens, user is responsible to catch it.
|
||||
func (s *HTMLEngine) Load() error {
|
||||
if s.assetFn != nil && s.namesFn != nil {
|
||||
// NOT NECESSARY "fix" of https://github.com/kataras/iris/issues/784,
|
||||
// IT'S BAD CODE WRITTEN WE KEEP HERE ONLY FOR A REMINDER
|
||||
// for any future questions.
|
||||
//
|
||||
// if strings.HasPrefix(s.directory, "../") {
|
||||
// // this and some more additions are fixes for https://github.com/kataras/iris/issues/784
|
||||
// // however, the dev SHOULD
|
||||
// // run the go-bindata command from the "$dir" parent directory
|
||||
// // and just use the ./$dir in the declaration,
|
||||
// // so all these fixes are not really necessary but they are here
|
||||
// // for the future
|
||||
// dir, err := filepath.Abs(s.directory)
|
||||
// // the absolute dir here can be invalid if running from other
|
||||
// // folder but we really don't care
|
||||
// // when we're working with the bindata because
|
||||
// // we only care for its relative directory
|
||||
// // see `loadAssets` for more.
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// s.directory = dir
|
||||
// }
|
||||
|
||||
// embedded
|
||||
return s.loadAssets()
|
||||
}
|
||||
|
@ -269,34 +292,67 @@ func (s *HTMLEngine) loadAssets() error {
|
|||
}
|
||||
|
||||
for _, path := range names {
|
||||
// if filepath.IsAbs(virtualDirectory) {
|
||||
// // fixes https://github.com/kataras/iris/issues/784
|
||||
// // we take the absolute fullpath of the template file.
|
||||
// pathFileAbs, err := filepath.Abs(path)
|
||||
// if err != nil {
|
||||
// templateErr = err
|
||||
// continue
|
||||
// }
|
||||
//
|
||||
// path = pathFileAbs
|
||||
// }
|
||||
|
||||
// bindata may contain more files than the templates
|
||||
// so keep that check as it's.
|
||||
if !strings.HasPrefix(path, virtualDirectory) {
|
||||
continue
|
||||
}
|
||||
|
||||
ext := filepath.Ext(path)
|
||||
// check if extension matches
|
||||
if ext == virtualExtension {
|
||||
|
||||
// take the relative path of the path as base of
|
||||
// virtualDirectory (the absolute path of the view engine that dev passed).
|
||||
rel, err := filepath.Rel(virtualDirectory, path)
|
||||
if err != nil {
|
||||
templateErr = err
|
||||
return err
|
||||
continue
|
||||
}
|
||||
|
||||
// // take the current working directory
|
||||
// cpath, err := filepath.Abs(".")
|
||||
// if err == nil {
|
||||
// // set the path as relative to "path" of the current working dir.
|
||||
// // fixes https://github.com/kataras/iris/issues/784
|
||||
// rpath, err := filepath.Rel(cpath, path)
|
||||
// // fix view: Asset not found for path ''
|
||||
// if err == nil && rpath != "" {
|
||||
// path = rpath
|
||||
// }
|
||||
// }
|
||||
|
||||
buf, err := assetFn(path)
|
||||
if err != nil {
|
||||
templateErr = err
|
||||
return err
|
||||
templateErr = fmt.Errorf("%v for path '%s'", err, path)
|
||||
continue
|
||||
}
|
||||
|
||||
contents := string(buf)
|
||||
name := filepath.ToSlash(rel)
|
||||
|
||||
// name should be the filename of the template.
|
||||
tmpl := s.Templates.New(name)
|
||||
tmpl.Option(s.options...)
|
||||
|
||||
if s.middleware != nil {
|
||||
contents, err = s.middleware(name, contents)
|
||||
}
|
||||
if err != nil {
|
||||
templateErr = err
|
||||
return err
|
||||
if err != nil {
|
||||
templateErr = fmt.Errorf("%v for name '%s'", err, name)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Add our funcmaps.
|
||||
|
|
Loading…
Reference in New Issue
Block a user