Beego 模板中常用数据类型处理

PHP二次开发 Golang 174 次浏览 , , 没有评论

default.go

  1. package controllers
  2.  
  3. import (
  4. "github.com/astaxie/beego"
  5. )
  6.  
  7. type MainController struct {
  8. beego.Controller
  9. }
  10.  
  11. func (c *MainController) Get() {
  12. c.Data["Website"] = "beego.me"
  13. c.Data["Email"] = "astaxie@gmail.com"
  14. c.TplName = "index.tpl"
  15.  
  16. c.Data["TrueCond"] = true
  17. c.Data["FalseCond"] = false
  18.  
  19. type u struct {
  20. Name string
  21. Age int
  22. Sex string
  23. }
  24.  
  25. user := &u{
  26. Name: "Joe",
  27. Age: 20,
  28. Sex: "Male",
  29. }
  30.  
  31. c.Data["User"] = user
  32.  
  33. nums := []int{1,2,3,4,5,6,7,8,9,0}
  34. c.Data["Nums"] = nums
  35.  
  36. c.Data["TplVar"] = "hey gays"
  37.  
  38. c.Data["Html"] = "<div>Hello beego</div>"
  39.  
  40. c.Data["Pipe"] = "<div>Hello beego</div>"
  41. }

 

index.tpl

  1. <header>
  2. <h1 class="logo">Welcome to Beego</h1>
  3. <div class="description">
  4. Beego is a simple & powerful Go web framework which is inspired by tornado and sinatra.
  5. </div>
  6. <div>
  7. {{if .TrueCond}}
  8. true condition
  9. {{end}}
  10. </div>
  11. <div>
  12. {{if .FalseCond}}
  13. {{else}}
  14. false condition
  15. {{end}}
  16. </div>
  17. <div>
  18. {{with .User}}
  19. {{.Name}}; {{.Age}}; {{.Sex}}
  20. {{end}}
  21. </div>
  22. <div>
  23. {{range .Nums}}
  24. {{.}}
  25. {{end}}
  26. </div>
  27. <div>
  28. {{$tplVar := .TplVar}}
  29. {{$tplVar}}
  30. </div>
  31. <div>
  32. {{str2html .Html}}
  33. </div>
  34. <div>
  35. {{.Pipe | htmlquote}}
  36. </div>
  37. <div>
  38. {{template "test"}}
  39. </div>
  40. </header>
  41. <footer>
  42. <div class="author">
  43. Official website:
  44. <a href="http://{{.Website}}">{{.Website}}</a> /
  45. Contact me:
  46. <a class="email" href="mailto:{{.Email}}">{{.Email}}</a>
  47. </div>
  48. </footer>
  49. <div class="backdrop"></div>
  50.  
  51. <script src="/static/js/reload.min.js"></script>
  52. </body>
  53.  
  54.  
  55. {{define "test"}}
  56. <div>
  57. this is test template
  58. </div>
  59. {{end}}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Go