mirror of
https://github.com/kataras/iris.git
synced 2026-01-08 20:41:57 +00:00
Add notes for the new lead maintainer of the open-source iris project and align with @get-ion/ion by @hiveminded
Former-commit-id: da4f38eb9034daa49446df3ee529423b98f9b331
This commit is contained in:
56
_examples/view/context-view-data/main.go
Normal file
56
_examples/view/context-view-data/main.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultTitle = "My Awesome Site"
|
||||
DefaultLayout = "layouts/layout.html"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
// output startup banner and error logs on os.Stdout
|
||||
|
||||
// set the view engine target to ./templates folder
|
||||
app.RegisterView(iris.HTML("./templates", ".html").Reload(true))
|
||||
|
||||
app.Use(func(ctx context.Context) {
|
||||
// set the title, current time and a layout in order to be used if and when the next handler(s) calls the .Render function
|
||||
ctx.ViewData("Title", DefaultTitle)
|
||||
now := time.Now().Format(ctx.Application().ConfigurationReadOnly().GetTimeFormat())
|
||||
ctx.ViewData("CurrentTime", now)
|
||||
ctx.ViewLayout(DefaultLayout)
|
||||
|
||||
ctx.Next()
|
||||
})
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
ctx.ViewData("BodyMessage", "a sample text here... setted by the route handler")
|
||||
if err := ctx.View("index.html"); err != nil {
|
||||
ctx.Application().Logger().Infof(err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
app.Get("/about", func(ctx context.Context) {
|
||||
ctx.ViewData("Title", "My About Page")
|
||||
ctx.ViewData("BodyMessage", "about text here... setted by the route handler")
|
||||
|
||||
// same file, just to keep things simple.
|
||||
if err := ctx.View("index.html"); err != nil {
|
||||
ctx.Application().Logger().Infof(err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
// http://localhost:8080
|
||||
// http://localhost:8080/about
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
// Notes: ViewData("", myCustomStruct{}) will set this myCustomStruct value as a root binding data,
|
||||
// so any View("other", "otherValue") will probably fail.
|
||||
// To clear the binding data: ctx.Set(ctx.Application().ConfigurationReadOnly().GetViewDataContextKey(), nil)
|
||||
8
_examples/view/context-view-data/templates/index.html
Normal file
8
_examples/view/context-view-data/templates/index.html
Normal file
@@ -0,0 +1,8 @@
|
||||
<h1>
|
||||
Title: {{.Title}}
|
||||
</h1>
|
||||
<h3>{{.BodyMessage}} </h3>
|
||||
|
||||
<hr/>
|
||||
|
||||
Current time: {{.CurrentTime}}
|
||||
@@ -0,0 +1,10 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>My WebsiteLayout</title>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<!-- Render the current template here -->
|
||||
{{ yield }}
|
||||
</body>
|
||||
</html>
|
||||
237
_examples/view/embedding-templates-into-app/bindata.go
Normal file
237
_examples/view/embedding-templates-into-app/bindata.go
Normal file
@@ -0,0 +1,237 @@
|
||||
// Code generated by go-bindata.
|
||||
// sources:
|
||||
// templates/hi.html
|
||||
// DO NOT EDIT!
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func bindataRead(data []byte, name string) ([]byte, error) {
|
||||
gz, err := gzip.NewReader(bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Read %q: %v", name, err)
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
_, err = io.Copy(&buf, gz)
|
||||
clErr := gz.Close()
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Read %q: %v", name, err)
|
||||
}
|
||||
if clErr != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
type asset struct {
|
||||
bytes []byte
|
||||
info os.FileInfo
|
||||
}
|
||||
|
||||
type bindataFileInfo struct {
|
||||
name string
|
||||
size int64
|
||||
mode os.FileMode
|
||||
modTime time.Time
|
||||
}
|
||||
|
||||
func (fi bindataFileInfo) Name() string {
|
||||
return fi.name
|
||||
}
|
||||
func (fi bindataFileInfo) Size() int64 {
|
||||
return fi.size
|
||||
}
|
||||
func (fi bindataFileInfo) Mode() os.FileMode {
|
||||
return fi.mode
|
||||
}
|
||||
func (fi bindataFileInfo) ModTime() time.Time {
|
||||
return fi.modTime
|
||||
}
|
||||
func (fi bindataFileInfo) IsDir() bool {
|
||||
return false
|
||||
}
|
||||
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")
|
||||
|
||||
func templatesHiHtmlBytes() ([]byte, error) {
|
||||
return bindataRead(
|
||||
_templatesHiHtml,
|
||||
"templates/hi.html",
|
||||
)
|
||||
}
|
||||
|
||||
func templatesHiHtml() (*asset, error) {
|
||||
bytes, err := templatesHiHtmlBytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := bindataFileInfo{name: "templates/hi.html", size: 97, mode: os.FileMode(438), modTime: time.Unix(1496244983, 0)}
|
||||
a := &asset{bytes: bytes, info: info}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
// Asset loads and returns the asset for the given name.
|
||||
// It returns an error if the asset could not be found or
|
||||
// could not be loaded.
|
||||
func Asset(name string) ([]byte, error) {
|
||||
cannonicalName := strings.Replace(name, "\\", "/", -1)
|
||||
if f, ok := _bindata[cannonicalName]; ok {
|
||||
a, err := f()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err)
|
||||
}
|
||||
return a.bytes, nil
|
||||
}
|
||||
return nil, fmt.Errorf("Asset %s not found", name)
|
||||
}
|
||||
|
||||
// MustAsset is like Asset but panics when Asset would return an error.
|
||||
// It simplifies safe initialization of global variables.
|
||||
func MustAsset(name string) []byte {
|
||||
a, err := Asset(name)
|
||||
if err != nil {
|
||||
panic("asset: Asset(" + name + "): " + err.Error())
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// AssetInfo loads and returns the asset info for the given name.
|
||||
// It returns an error if the asset could not be found or
|
||||
// could not be loaded.
|
||||
func AssetInfo(name string) (os.FileInfo, error) {
|
||||
cannonicalName := strings.Replace(name, "\\", "/", -1)
|
||||
if f, ok := _bindata[cannonicalName]; ok {
|
||||
a, err := f()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("AssetInfo %s can't read by error: %v", name, err)
|
||||
}
|
||||
return a.info, nil
|
||||
}
|
||||
return nil, fmt.Errorf("AssetInfo %s not found", name)
|
||||
}
|
||||
|
||||
// AssetNames returns the names of the assets.
|
||||
func AssetNames() []string {
|
||||
names := make([]string, 0, len(_bindata))
|
||||
for name := range _bindata {
|
||||
names = append(names, name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// _bindata is a table, holding each asset generator, mapped to its name.
|
||||
var _bindata = map[string]func() (*asset, error){
|
||||
"templates/hi.html": templatesHiHtml,
|
||||
}
|
||||
|
||||
// AssetDir returns the file names below a certain
|
||||
// directory embedded in the file by go-bindata.
|
||||
// For example if you run go-bindata on data/... and data contains the
|
||||
// following hierarchy:
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
// then AssetDir("data") would return []string{"foo.txt", "img"}
|
||||
// AssetDir("data/img") would return []string{"a.png", "b.png"}
|
||||
// AssetDir("foo.txt") and AssetDir("notexist") would return an error
|
||||
// AssetDir("") will return []string{"data"}.
|
||||
func AssetDir(name string) ([]string, error) {
|
||||
node := _bintree
|
||||
if len(name) != 0 {
|
||||
cannonicalName := strings.Replace(name, "\\", "/", -1)
|
||||
pathList := strings.Split(cannonicalName, "/")
|
||||
for _, p := range pathList {
|
||||
node = node.Children[p]
|
||||
if node == nil {
|
||||
return nil, fmt.Errorf("Asset %s not found", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
if node.Func != nil {
|
||||
return nil, fmt.Errorf("Asset %s not found", name)
|
||||
}
|
||||
rv := make([]string, 0, len(node.Children))
|
||||
for childName := range node.Children {
|
||||
rv = append(rv, childName)
|
||||
}
|
||||
return rv, nil
|
||||
}
|
||||
|
||||
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{}},
|
||||
}},
|
||||
}}
|
||||
|
||||
// RestoreAsset restores an asset under the given directory
|
||||
func RestoreAsset(dir, name string) error {
|
||||
data, err := Asset(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
info, err := AssetInfo(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.MkdirAll(_filePath(dir, filepath.Dir(name)), os.FileMode(0755))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RestoreAssets restores an asset under the given directory recursively
|
||||
func RestoreAssets(dir, name string) error {
|
||||
children, err := AssetDir(name)
|
||||
// File
|
||||
if err != nil {
|
||||
return RestoreAsset(dir, name)
|
||||
}
|
||||
// Dir
|
||||
for _, child := range children {
|
||||
err = RestoreAssets(dir, filepath.Join(name, child))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func _filePath(dir, name string) string {
|
||||
cannonicalName := strings.Replace(name, "\\", "/", -1)
|
||||
return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...)
|
||||
}
|
||||
29
_examples/view/embedding-templates-into-app/main.go
Normal file
29
_examples/view/embedding-templates-into-app/main.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
// $ 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)
|
||||
|
||||
// http://localhost:8080
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
type page struct {
|
||||
Title, Name string
|
||||
}
|
||||
|
||||
func hi(ctx context.Context) {
|
||||
ctx.ViewData("", page{Title: "Hi Page", Name: "iris"})
|
||||
ctx.View("hi.html")
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>{{.Title}}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hi {{.Name}}
|
||||
</body>
|
||||
</html>
|
||||
47
_examples/view/overview/main.go
Normal file
47
_examples/view/overview/main.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
// - standard html | iris.HTML(...)
|
||||
// - django | iris.Django(...)
|
||||
// - pug(jade) | iris.Pug(...)
|
||||
// - handlebars | iris.Handlebars(...)
|
||||
// - amber | iris.Amber(...)
|
||||
// with default template funcs:
|
||||
//
|
||||
// - {{ urlpath "mynamedroute" "pathParameter_ifneeded" }}
|
||||
// - {{ render "header.html" }}
|
||||
// - {{ render_r "header.html" }} // partial relative path to current page
|
||||
// - {{ yield }}
|
||||
// - {{ current }}
|
||||
app.RegisterView(iris.HTML("./templates", ".html"))
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
|
||||
ctx.ViewData("Name", "iris") // the .Name inside the ./templates/hi.html
|
||||
ctx.Gzip(true) // enable gzip for big files
|
||||
ctx.View("hi.html") // render the template with the file name relative to the './templates'
|
||||
|
||||
})
|
||||
|
||||
// http://localhost:8080/
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
/*
|
||||
Note:
|
||||
|
||||
In case you're wondering, the code behind the view engines derives from the "github.com/kataras/iris/view" package,
|
||||
access to the engines' variables can be granded by "github.com/kataras/iris" package too.
|
||||
|
||||
iris.HTML(...) is a shortcut of view.HTML(...)
|
||||
iris.Django(...) >> >> view.Django(...)
|
||||
iris.Pug(...) >> >> view.Pug(...)
|
||||
iris.Handlebars(...) >> >> view.Handlebars(...)
|
||||
iris.Amber(...) >> >> view.Amber(...)
|
||||
*/
|
||||
11
_examples/view/overview/templates/hi.html
Normal file
11
_examples/view/overview/templates/hi.html
Normal file
@@ -0,0 +1,11 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Hi iris</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Hi {{.Name}} </h1>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
55
_examples/view/template_html_0/main.go
Normal file
55
_examples/view/template_html_0/main.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New() // defaults to these
|
||||
|
||||
// - standard html | iris.HTML(...)
|
||||
// - django | iris.Django(...)
|
||||
// - pug(jade) | iris.Pug(...)
|
||||
// - handlebars | iris.Handlebars(...)
|
||||
// - amber | iris.Amber(...)
|
||||
|
||||
tmpl := iris.HTML("./templates", ".html")
|
||||
tmpl.Reload(true) // reload templates on each request (development mode)
|
||||
// default template funcs are:
|
||||
//
|
||||
// - {{ urlpath "mynamedroute" "pathParameter_ifneeded" }}
|
||||
// - {{ render "header.html" }}
|
||||
// - {{ render_r "header.html" }} // partial relative path to current page
|
||||
// - {{ yield }}
|
||||
// - {{ current }}
|
||||
tmpl.AddFunc("greet", func(s string) string {
|
||||
return "Greetings " + s + "!"
|
||||
})
|
||||
app.RegisterView(tmpl)
|
||||
|
||||
app.Get("/", hi)
|
||||
|
||||
// http://localhost:8080
|
||||
app.Run(iris.Addr(":8080"), iris.WithCharset("UTF-8")) // defaults to that but you can change it.
|
||||
}
|
||||
|
||||
func hi(ctx context.Context) {
|
||||
ctx.ViewData("Title", "Hi Page")
|
||||
ctx.ViewData("Name", "iris") // {{.Name}} will render: iris
|
||||
// ctx.ViewData("", myCcustomStruct{})
|
||||
ctx.View("hi.html")
|
||||
}
|
||||
|
||||
/*
|
||||
Note:
|
||||
|
||||
In case you're wondering, the code behind the view engines derives from the "github.com/kataras/iris/view" package,
|
||||
access to the engines' variables can be granded by "github.com/kataras/iris" package too.
|
||||
|
||||
iris.HTML(...) is a shortcut of view.HTML(...)
|
||||
iris.Django(...) >> >> view.Django(...)
|
||||
iris.Pug(...) >> >> view.Pug(...)
|
||||
iris.Handlebars(...) >> >> view.Handlebars(...)
|
||||
iris.Amber(...) >> >> view.Amber(...)
|
||||
*/
|
||||
8
_examples/view/template_html_0/templates/hi.html
Normal file
8
_examples/view/template_html_0/templates/hi.html
Normal file
@@ -0,0 +1,8 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>{{.Title}}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hi {{.Name}} </h1>
|
||||
</body>
|
||||
</html>
|
||||
30
_examples/view/template_html_1/main.go
Normal file
30
_examples/view/template_html_1/main.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
type mypage struct {
|
||||
Title string
|
||||
Message string
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.RegisterView(iris.HTML("./templates", ".html").Layout("layout.html"))
|
||||
// TIP: append .Reload(true) to reload the templates on each request.
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
ctx.Gzip(true)
|
||||
ctx.ViewData("", mypage{"My Page title", "Hello world!"})
|
||||
ctx.View("mypage.html")
|
||||
// Note that: you can pass "layout" : "otherLayout.html" to bypass the config's Layout property
|
||||
// or view.NoLayout to disable layout on this render action.
|
||||
// third is an optional parameter
|
||||
})
|
||||
|
||||
// http://localhost:8080
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
11
_examples/view/template_html_1/templates/layout.html
Normal file
11
_examples/view/template_html_1/templates/layout.html
Normal file
@@ -0,0 +1,11 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>My Layout</title>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<h1>[layout] Body content is below...</h1>
|
||||
<!-- Render the current template here -->
|
||||
{{ yield }}
|
||||
</body>
|
||||
</html>
|
||||
4
_examples/view/template_html_1/templates/mypage.html
Normal file
4
_examples/view/template_html_1/templates/mypage.html
Normal file
@@ -0,0 +1,4 @@
|
||||
<h1>
|
||||
Title: {{.Title}}
|
||||
</h1>
|
||||
<h3>Message: {{.Message}} </h3>
|
||||
3
_examples/view/template_html_2/README.md
Normal file
3
_examples/view/template_html_2/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
## Info
|
||||
|
||||
This folder examines the {{render "dir/templatefilename"}} functionality to manually render any template inside any template
|
||||
51
_examples/view/template_html_2/main.go
Normal file
51
_examples/view/template_html_2/main.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
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 + "!"
|
||||
})
|
||||
|
||||
app.RegisterView(tmpl)
|
||||
|
||||
app.Get("/", func(ctx context.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 context.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 context.Context) {
|
||||
ctx.View("page1.html")
|
||||
})
|
||||
my.Get("/other", func(ctx context.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"))
|
||||
}
|
||||
12
_examples/view/template_html_2/templates/layouts/layout.html
Normal file
12
_examples/view/template_html_2/templates/layouts/layout.html
Normal file
@@ -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>
|
||||
7
_examples/view/template_html_2/templates/page1.html
Normal file
7
_examples/view/template_html_2/templates/page1.html
Normal file
@@ -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>
|
||||
68
_examples/view/template_html_3/main.go
Normal file
68
_examples/view/template_html_3/main.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// Package main an example on how to naming your routes & use the custom 'url path' HTML Template Engine, same for other template engines.
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.RegisterView(iris.HTML("./templates", ".html").Reload(true))
|
||||
|
||||
mypathRoute := app.Get("/mypath", writePathHandler)
|
||||
mypathRoute.Name = "my-page1"
|
||||
|
||||
mypath2Route := app.Get("/mypath2/{paramfirst}/{paramsecond}", writePathHandler)
|
||||
mypath2Route.Name = "my-page2"
|
||||
|
||||
mypath3Route := app.Get("/mypath3/{paramfirst}/statichere/{paramsecond}", writePathHandler)
|
||||
mypath3Route.Name = "my-page3"
|
||||
|
||||
mypath4Route := app.Get("/mypath4/{paramfirst}/statichere/{paramsecond}/{otherparam}/{something:path}", writePathHandler)
|
||||
// same as: app.Get("/mypath4/:paramfirst/statichere/:paramsecond/:otherparam/*something", writePathHandler)
|
||||
mypath4Route.Name = "my-page4"
|
||||
|
||||
// same with Handle/Func
|
||||
mypath5Route := app.Handle("GET", "/mypath5/{paramfirst}/statichere/{paramsecond}/{otherparam}/anything/{something:path}", writePathHandler)
|
||||
mypath5Route.Name = "my-page5"
|
||||
|
||||
mypath6Route := app.Get("/mypath6/{paramfirst}/{paramsecond}/statichere/{paramThirdAfterStatic}", writePathHandler)
|
||||
mypath6Route.Name = "my-page6"
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
// for /mypath6...
|
||||
paramsAsArray := []string{"theParam1", "theParam2", "paramThirdAfterStatic"}
|
||||
ctx.ViewData("ParamsAsArray", paramsAsArray)
|
||||
if err := ctx.View("page.html"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
|
||||
app.Get("/redirect/{namedRoute}", func(ctx context.Context) {
|
||||
routeName := ctx.Params().Get("namedRoute")
|
||||
r := app.GetRoute(routeName)
|
||||
if r == nil {
|
||||
ctx.StatusCode(404)
|
||||
ctx.Writef("Route with name %s not found", routeName)
|
||||
return
|
||||
}
|
||||
|
||||
println("The path of " + routeName + "is: " + r.Path)
|
||||
// if routeName == "my-page1"
|
||||
// prints: The path of of my-page1 is: /mypath
|
||||
// if it's a path which takes named parameters
|
||||
// then use "r.ResolvePath(paramValuesHere)"
|
||||
ctx.Redirect(r.Path)
|
||||
// http://localhost:8080/redirect/my-page1 will redirect to -> http://localhost:8080/mypath
|
||||
})
|
||||
|
||||
// http://localhost:8080
|
||||
// http://localhost/redirect/my-page1
|
||||
app.Run(iris.Addr(":8080"))
|
||||
|
||||
}
|
||||
|
||||
func writePathHandler(ctx context.Context) {
|
||||
ctx.Writef("Hello from %s.", ctx.Path())
|
||||
}
|
||||
25
_examples/view/template_html_3/templates/page.html
Normal file
25
_examples/view/template_html_3/templates/page.html
Normal file
@@ -0,0 +1,25 @@
|
||||
<a href="{{urlpath "my-page1"}}">/mypath</a>
|
||||
<br />
|
||||
<br />
|
||||
|
||||
<a href="{{urlpath "my-page2" "theParam1" "theParam2"}}">/mypath2/{paramfirst}/{paramsecond}</a>
|
||||
<br />
|
||||
<br />
|
||||
|
||||
<a href="{{urlpath "my-page3" "theParam1" "theParam2AfterStatic"}}">/mypath3/{paramfirst}/statichere/{paramsecond}</a>
|
||||
<br />
|
||||
<br />
|
||||
|
||||
<a href="{{urlpath "my-page4" "theParam1" "theparam2AfterStatic" "otherParam" "matchAnything"}}">
|
||||
/mypath4/{paramfirst}/statichere/{paramsecond}/{otherparam}/{something:path}</a>
|
||||
<br />
|
||||
<br />
|
||||
|
||||
<a href="{{urlpath "my-page5" "theParam1" "theParam2Afterstatichere" "otherParam" "matchAnythingAfterStatic"}}">
|
||||
/mypath5/{paramfirst}/statichere/{paramsecond}/{otherparam}/anything/{anything:path}</a>
|
||||
<br />
|
||||
<br />
|
||||
|
||||
<a href={{urlpath "my-page6" .ParamsAsArray }}>
|
||||
/mypath6/{paramfirst}/{paramsecond}/statichere/{paramThirdAfterStatic}
|
||||
</a>
|
||||
32
_examples/view/template_html_4/hosts
Normal file
32
_examples/view/template_html_4/hosts
Normal file
@@ -0,0 +1,32 @@
|
||||
# Copyright (c) 1993-2009 Microsoft Corp.
|
||||
#
|
||||
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
|
||||
#
|
||||
# This file contains the mappings of IP addresses to host names. Each
|
||||
# entry should be kept on an individual line. The IP address should
|
||||
# be placed in the first column followed by the corresponding host name.
|
||||
# The IP address and the host name should be separated by at least one
|
||||
# space.
|
||||
#
|
||||
# Additionally, comments (such as these) may be inserted on individual
|
||||
# lines or following the machine name denoted by a '#' symbol.
|
||||
#
|
||||
# For example:
|
||||
#
|
||||
# 102.54.94.97 rhino.acme.com # source server
|
||||
# 38.25.63.10 x.acme.com # x client host
|
||||
|
||||
# localhost name resolution is handled within DNS itself.
|
||||
127.0.0.1 localhost
|
||||
::1 localhost
|
||||
#-iris-For development machine, you have to configure your dns also for online, search google how to do it if you don't know
|
||||
|
||||
127.0.0.1 username1.127.0.0.1
|
||||
127.0.0.1 username2.127.0.0.1
|
||||
127.0.0.1 username3.127.0.0.1
|
||||
127.0.0.1 username4.127.0.0.1
|
||||
127.0.0.1 username5.127.0.0.1
|
||||
# note that you can always use custom subdomains
|
||||
#-END iris-
|
||||
|
||||
# Windows: Drive:/Windows/system32/drivers/etc/hosts, on Linux: /etc/hosts
|
||||
71
_examples/view/template_html_4/main.go
Normal file
71
_examples/view/template_html_4/main.go
Normal file
@@ -0,0 +1,71 @@
|
||||
// Package main an example on how to naming your routes & use the custom 'url' HTML Template Engine, same for other template engines.
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/core/router"
|
||||
)
|
||||
|
||||
const (
|
||||
host = "127.0.0.1:8080"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
// create a custom path reverser, iris let you define your own host and scheme
|
||||
// which is useful when you have nginx or caddy in front of iris.
|
||||
rv := router.NewRoutePathReverser(app, router.WithHost(host), router.WithScheme("http"))
|
||||
// locate and define our templates as usual.
|
||||
templates := iris.HTML("./templates", ".html")
|
||||
// add a custom func of "url" and pass the rv.URL as its template function body,
|
||||
// so {{url "routename" "paramsOrSubdomainAsFirstArgument"}} will work inside our templates.
|
||||
templates.AddFunc("url", rv.URL)
|
||||
|
||||
app.RegisterView(templates)
|
||||
|
||||
// wildcard subdomain, will catch username1.... username2.... username3... username4.... username5...
|
||||
// that our below links are providing via page.html's first argument which is the subdomain.
|
||||
|
||||
subdomain := app.Party("*.")
|
||||
|
||||
mypathRoute := subdomain.Get("/mypath", emptyHandler)
|
||||
mypathRoute.Name = "my-page1"
|
||||
|
||||
mypath2Route := subdomain.Get("/mypath2/{paramfirst}/{paramsecond}", emptyHandler)
|
||||
mypath2Route.Name = "my-page2"
|
||||
|
||||
mypath3Route := subdomain.Get("/mypath3/{paramfirst}/statichere/{paramsecond}", emptyHandler)
|
||||
mypath3Route.Name = "my-page3"
|
||||
|
||||
mypath4Route := subdomain.Get("/mypath4/{paramfirst}/statichere/{paramsecond}/{otherparam}/{something:path}", emptyHandler)
|
||||
mypath4Route.Name = "my-page4"
|
||||
|
||||
mypath5Route := subdomain.Handle("GET", "/mypath5/{paramfirst}/statichere/{paramsecond}/{otherparam}/anything/{something:path}", emptyHandler)
|
||||
mypath5Route.Name = "my-page5"
|
||||
|
||||
mypath6Route := subdomain.Get("/mypath6/{paramfirst}/{paramsecond}/staticParam/{paramThirdAfterStatic}", emptyHandler)
|
||||
mypath6Route.Name = "my-page6"
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
// for username5./mypath6...
|
||||
paramsAsArray := []string{"username5", "theParam1", "theParam2", "paramThirdAfterStatic"}
|
||||
ctx.ViewData("ParamsAsArray", paramsAsArray)
|
||||
if err := ctx.View("page.html"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
|
||||
// http://127.0.0.1:8080
|
||||
app.Run(iris.Addr(host))
|
||||
}
|
||||
|
||||
func emptyHandler(ctx context.Context) {
|
||||
ctx.Writef("Hello from subdomain: %s , you're in path: %s", ctx.Subdomain(), ctx.Path())
|
||||
}
|
||||
|
||||
// Note:
|
||||
// If you got an empty string on {{ url }} or {{ urlpath }} it means that
|
||||
// args length are not aligned with the route's parameters length
|
||||
// or the route didn't found by the passed name.
|
||||
29
_examples/view/template_html_4/templates/page.html
Normal file
29
_examples/view/template_html_4/templates/page.html
Normal file
@@ -0,0 +1,29 @@
|
||||
<!-- the only difference between normal named routes and dynamic subdomains named routes is that the first argument of url
|
||||
is the subdomain part instead of named parameter-->
|
||||
|
||||
<a href="{{url "my-page1" "username1"}}">username1.127.0.0.1:8080/mypath</a>
|
||||
<br />
|
||||
<br />
|
||||
<a href="{{url "my-page2" "username2" "theParam1" "theParam2"}}">
|
||||
username2.127.0.0.1:8080/mypath2/{paramfirst}/{paramsecond}
|
||||
</a>
|
||||
<br />
|
||||
<br />
|
||||
<a href="{{url "my-page3" "username3" "theParam1" "theParam2AfterStatic"}}">
|
||||
username3.127.0.0.1:8080/mypath3/{paramfirst}/statichere/{paramsecond}
|
||||
</a>
|
||||
<br />
|
||||
<br />
|
||||
<a href="{{url "my-page4" "username4" "theParam1" "theparam2AfterStatic" "otherParam" "matchAnything"}}">
|
||||
username4.127.0.0.1:8080/mypath4/{paramfirst}/statichere/{paramsecond}/{otherParam}/{something:path}
|
||||
</a>
|
||||
<br />
|
||||
<br />
|
||||
<a href="{{url "my-page5" "username5" "theParam1" "theparam2AfterStatic" "otherParam" "matchAnything"}}">
|
||||
username5.127.0.0.1:8080/mypath5/{paramfirst}/statichere/{paramsecond}/{otherparam}/anything/{something:path}
|
||||
</a>
|
||||
<br/>
|
||||
<br/>
|
||||
<a href="{{url "my-page6" .ParamsAsArray }}">
|
||||
username5.127.0.0.1:8080/mypath6/{paramfirst}/{paramsecond}/staticParam/{paramThirdAfterStatic}
|
||||
</a>
|
||||
Reference in New Issue
Block a user