码迷,mamicode.com
首页 > 其他好文 > 详细

go revel 初探一

时间:2015-02-26 19:55:52      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

1.app/controllers/app.go控制器

package controllers

import "github.com/revel/revel"

type App struct {
    *revel.Controller
}

func (c App) Index() revel.Result {
    str := "传递一个变量到模版看看"
    return c.Render(str)
}

func (c App) Hello(Name string) revel.Result {
    c.Validation.Required(Name).Message("名字必填")
    if c.Validation.HasErrors() {
        c.Validation.Keep()
        c.FlashParams()
        return c.Redirect(App.Index)
    }
    return c.Render(Name)
}

2.app/views/App/Index.html模版文件(对应的controller为app.go文件的Index方法)

{{set . "title" "Home"}}
{{template "header.html" .}}

<header class="hero-unit" style="background-color:#A9F16C">
  <div class="container">
    <div class="row">
      <div class="hero-text">
        <h1>It works 啊哈哈哈! {{.str}}</h1>
        <p></p>
      </div>
    </div>
  </div>
</header>

{{range .errors}}
<p style="color:#c00">{{.Message}}</p>
{{end}}
<form action="/App/Hello" method="GET">
    <input type="text" name="Name" value="{{.flash.Name}}" />
    <input type="submit" value="提交" />
</form>
{{template "footer.html" .}}

app/views/App/Hello.html模版文件(对应的controller为app.go文件的Hello方法)

{{set . "title" "Home"}}
{{template "header.html" .}}

<h1>Hello {{.Name}}</h1>
<a href="/">返回</a>

{{template "footer.html" .}}

 

go revel 初探一

标签:

原文地址:http://www.cnblogs.com/benlightning/p/4301885.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!