资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Beego模板相关用法-创新互联

1、创建beeblog项目

➜  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

2、自定义数据结构

➜  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

  
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}}

3、with end使用

注意:为了解决循环嵌套的问题,可以使用with end的方式

创新互联公司专注于企业网络营销推广、网站重做改版、麦盖提网站定制设计、自适应品牌网站建设、HTML5建站电子商务商城网站建设、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为麦盖提等各大城市提供网站开发制作服务。
{{.User.Name}}; {{.User.Age}}; {{.User.Sex}}
这段代码可以修改为如下,效果相同:
{{with .User}} {{.Name}}; {{.Age}}; {{.Sex}} {{end}}

➜ beeblog bee run beeblog
访问:http://localhost:8080/
Beego模板相关用法

4、range使用

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/
Beego模板相关用法

5、自定义模板

default.go 添加:
    c.Data["TplVar"] = "hey guys"

index.tpl中添加:
  
{{$tplVar := .TplVar}} {{$tplVar}}

访问:http://localhost:8080/
Beego模板相关用法

6、beego内置模板函数使用,以字符串转html模板函数

beego中写的任意html的代码,它都会自动编码

default.go 添加:
        c.Data.["Html"]="
hello beego
" index.tpl中添加:
{{.Html}}

访问:http://localhost:8080/ 返回:

hello beego
,怎么以html格式显示呢?
Beego模板相关用法

index.tpl中:
    
{{.Html}}
修改为如下即可:
{{str2html .Html}}

再访问:http://localhost:8080/ 结果:
Beego模板相关用法

7、pipeline使用(查看数据格式)

default.go 添加:
            c.Data["Pipe"] = "
hello beego
" index.tpl中添加:
{{.Pipe | htmlquote}}

访问:http://localhost:8080/ 结果:
返回:

hello beego

Beego模板相关用法

8、模板嵌套(类似函数调用)

{{template "test"}}
{{define "test"}}
this is test template
{{end}}

访问:http://localhost:8080/ 结果:
Beego模板相关用法

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


网站名称:Beego模板相关用法-创新互联
文章起源:http://cdkjz.cn/article/dodceh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220