➜ go pwd
/Users/daixuan/qbox/go
➜ go bee new beeblog
______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v1.10.0
2018/07/29 17:20:41 WARN ▶ 0001 You current workdir is not inside $GOPATH/src.
2018/07/29 17:20:41 INFO ▶ 0002 Creating application...
create /Users/daixuan/qbox/go/src/beeblog/
create /Users/daixuan/qbox/go/src/beeblog/conf/
create /Users/daixuan/qbox/go/src/beeblog/controllers/
create /Users/daixuan/qbox/go/src/beeblog/models/
create /Users/daixuan/qbox/go/src/beeblog/routers/
create /Users/daixuan/qbox/go/src/beeblog/tests/
create /Users/daixuan/qbox/go/src/beeblog/static/
create /Users/daixuan/qbox/go/src/beeblog/static/js/
create /Users/daixuan/qbox/go/src/beeblog/static/css/
create /Users/daixuan/qbox/go/src/beeblog/static/img/
create /Users/daixuan/qbox/go/src/beeblog/views/
create /Users/daixuan/qbox/go/src/beeblog/conf/app.conf
create /Users/daixuan/qbox/go/src/beeblog/controllers/default.go
create /Users/daixuan/qbox/go/src/beeblog/views/index.tpl
create /Users/daixuan/qbox/go/src/beeblog/routers/router.go
create /Users/daixuan/qbox/go/src/beeblog/tests/default_test.go
create /Users/daixuan/qbox/go/src/beeblog/main.go
2018/07/29 17:20:41 SUCCESS ▶ 0003 New application successfully created!
➜ go ll /Users/daixuan/qbox/go/src
total 0
drwxr-xr-x 10 daixuan staff 320 Jul 29 17:20 beeblog
➜ beeblog vim /Users/daixuan/qbox/go/src/beeblog/controllers/default.go
package controllers
import (
"github.com/astaxie/beego"
)
type MainController struct {
beego.Controller
}
func (c *MainController) Get() {
c.Data["Website"] = "beego.me"
c.Data["Email"] = "astaxie@gmail.com"
c.TplName = "index.tpl"
c.Data["TrueCond"] = true
c.Data["FalseCond"] = false
//添加一个结构
type u struct{
Name string
Age int
Sex string
}
user := &u{
Name: "Joe",
Age: 20,
Sex: "Male",
}
c.Data["User"] = user
}
➜ beeblog vim /Users/daixuan/qbox/go/src/beeblog/views/index.tpl
Welcome to Beego
Beego is a simple & powerful Go web framework which is inspired by tornado and sinatra.
{{if .TrueCond}}
true condition
{{end}}
{{if .FalseCond}}
{{else}}
false condition.
{{end}}
{{.User.Name}}; {{.User.Age}}; {{.User.Sex}}
注意:为了解决循环嵌套的问题,可以使用with end的方式
10年积累的成都网站建设、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计制作后付款的网站建设流程,更有白沙黎族免费网站建设让你可以放心的选择与我们合作。
{{.User.Name}}; {{.User.Age}}; {{.User.Sex}}
这段代码可以修改为如下,效果相同:
{{with .User}}
{{.Name}}; {{.Age}}; {{.Sex}}
{{end}}
➜ beeblog bee run beeblog
访问:http://localhost:8080/
default.go 添加:
nums := []int{1,2,3,4,5,6,7,8,9,0}
c.Data["Nums"] = nums
index.tpl中添加:
{{.Nums}}
{{range .Nums}}
{{.}}
{{end}}
访问:http://localhost:8080/
default.go 添加:
c.Data["TplVar"] = "hey guys"
index.tpl中添加:
{{$tplVar := .TplVar}}
{{$tplVar}}
访问:http://localhost:8080/
beego中写的任意html的代码,它都会自动编码
default.go 添加:
c.Data.["Html"]="hello beego"
index.tpl中添加:
{{.Html}}
访问:http://localhost:8080/ 返回:
index.tpl中:
{{.Html}}
修改为如下即可:
{{str2html .Html}}
再访问:http://localhost:8080/ 结果:
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。