File: view/template_html_4/hosts
~~~
# 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
~~~
File: view/template_html_4/main.go
~~~
// 打包主要说明如何命名路由并使用自定义“url”HTML模板引擎,与其他模板引擎相同.
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/core/router"
)
const (
host = "127.0.0.1:8080"
)
func main() {
app := iris.New()
// 创建自定义路径反转器,Iiris让您定义自己的主机和方案
// 当你在iris前面有nginx或球童时很有用.
rv := router.NewRoutePathReverser(app, router.WithHost(host), router.WithScheme("http"))
//像往常一样定位和定义模板.
templates := iris.HTML("./templates", ".html")
// 添加“url”的自定义函数并将rv.URL作为其模板函数体传递,
// so {{url "routename" "paramsOrSubdomainAsFirstArgument"}}将在我们的模板中工作.
templates.AddFunc("url", rv.URL)
app.RegisterView(templates)
//通配符子域,将捕获 username1.... username2.... username3... username4.... username5...
// 我们的下面的链接是通过page.html的第一个参数提供的,它是子域名.
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 iris.Context) {
// for username5./mypath6...
paramsAsArray := []string{"username5", "theParam1", "theParam2", "paramThirdAfterStatic"}
ctx.ViewData("ParamsAsArray", paramsAsArray)
if err := ctx.View("page.html"); err != nil {
panic(err)
}
})
//我们的下面的链接是通过page.html的第一个参数提供的,它是子域名,
//在它看来它使用 {{urlpath ...}}
//为了向您展示您可以使用它
//如果您不希望整个方案和主机成为网址的一部分.
app.Get("/mypath7/{paramfirst}/{paramsecond}/static/{paramthird}", emptyHandler).Name = "my-page7"
// http://127.0.0.1:8080
app.Run(iris.Addr(host))
}
func emptyHandler(ctx iris.Context) {
ctx.Writef("Hello from subdomain: %s , you're in path: %s", ctx.Subdomain(), ctx.Path())
}
//注意:
//如果{{url}}或{{urlpath}}上有空字符串,则意味着
// args length与路径的参数长度不对齐
//或者通过名称找不到路由..
~~~
File: view/template_html_4/templates/page.html
~~~
<!-- 普通命名路由和名为routes的动态子域之间的唯一区别是url的第一个参数 是子域部分而不是命名参数-->
<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>
<br/>
<br/>
<a href="{{urlpath "my-page7" "theParam1" "theParam2" "theParam3" }}">
mypath7/{paramfirst}/{paramsecond}/static/{paramthird}
</a>
<br/>
<br/>
~~~
- api文档
- yaag
- 认证
- 认证基本
- oauth2
- 缓存
- 客户端
- 简单的
- 配置
- 配置文件
- tml文件
- yaml文件
- 功能
- 处理程序
- Negroni Like
- http
- Real Usecase Raven
- cookies
- Basic
- 安全cookie
- 实验程序
- casbin
- 云监视
- cors
- csrf
- jwt
- 新文学
- Casbin
- 文件服务器
- Hero
- http监听
- http请求
- HTTP Responsewriter
- Miscellaneous
- MVC
- mvc概观
- 中间件
- Hello World
- 登陆
- Session 控制器
- Singleton
- MVC基本
- ORM
- xorm
- 概况
- 路由
- 概观
- 自定义上下文
- 自定义包装
- 动态路径
- 相反
- HTTP错误
- 路由状态
- 路由基本
- Sessions
- 构建
- 子域
- 重定向
- 万维网
- Single
- 通配符
- 多域名
- 测试
- 教程
- 视图
- Template Html 0
- Template Html 1
- Template Html 2
- Template Html 3
- Template Html 4
- Webassembly
- Websocket
