mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 02:31:04 +01:00
Add an example for urlpath template func for Django engine
relative to #1656
This commit is contained in:
parent
546c7bf465
commit
dfe27567ae
|
@ -124,8 +124,10 @@
|
|||
* [Basic](view/template_html_0/main.go)
|
||||
* [A simple Layout](view/template_html_1/main.go)
|
||||
* [Layouts: `yield` and `render` tmpl funcs](view/template_html_2/main.go)
|
||||
* [The `urlpath` tmpl func](view/template_html_3/main.go)
|
||||
* [The `url` tmpl func](view/template_html_4/main.go)
|
||||
* The `urlpath` template func
|
||||
* [HTML](view/template_html_3/main.go)
|
||||
* [Django](view/template_django_1/main.go)
|
||||
* [The `url` template func](view/template_html_4/main.go)
|
||||
* [Inject Data Between Handlers](view/context-view-data/main.go)
|
||||
* [Inject Engine Between Handlers](view/context-view-engine/main.go)
|
||||
* [Embedding Templates Into App Executable File](view/embedding-templates-into-app/main.go)
|
||||
|
|
63
_examples/view/template_django_1/main.go
Normal file
63
_examples/view/template_django_1/main.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
package main
|
||||
|
||||
import "github.com/kataras/iris/v12"
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.RegisterView(iris.Django("./views", ".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 iris.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 iris.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:8080/redirect/my-page1
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func writePathHandler(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s.", ctx.Path())
|
||||
}
|
55
_examples/view/template_django_1/views/page.html
Normal file
55
_examples/view/template_django_1/views/page.html
Normal file
|
@ -0,0 +1,55 @@
|
|||
<html>
|
||||
|
||||
<head>
|
||||
<title>urlpath function - django</title>
|
||||
<style>
|
||||
a {
|
||||
color: #0f7afc;
|
||||
border-bottom-color: rgba(15, 122, 252, 0.2);
|
||||
text-decoration: none
|
||||
}
|
||||
|
||||
a:hover {
|
||||
color: #cf0000;
|
||||
border-bottom-color: rgba(208, 64, 0, 0.2);
|
||||
text-decoration: none
|
||||
}
|
||||
|
||||
a:visited {
|
||||
color: #800080;
|
||||
border-bottom-color: rgba(128, 0, 128, 0.2);
|
||||
text-decoration: none
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<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>
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -1,4 +1,5 @@
|
|||
// 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 an example on how to naming your routes & use the custom
|
||||
// 'url path' HTML Template Engine, same for other template engines.
|
||||
package main
|
||||
|
||||
import (
|
||||
|
|
Loading…
Reference in New Issue
Block a user