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" .}}
时间: 2024-10-11 11:25:52