go revel 初探一

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

 3.下面是header.html模版文件<!DOCTYPE html><html>

 <head> <title>{{.title}}</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" type="text/css" href="/public/css/bootstrap.css"> <link rel="shortcut icon" type="image/png" href="/public/img/favicon.png"> <script src="/public/js/jquery-1.9.1.min.js" type="text/javascript" charset="utf-8"></script>
<!-- 往moreStyles和moreScripts中追加样式和js地址,在/public/目录下 --> {{append . "moreStyles" "ui-lightness/jquery-ui-1.7.2.custom.css"}} {{append . "moreScripts" "js/jquery-ui-1.7.2.custom.min.js"}} {{range .moreStyles}} <link rel="stylesheet" type="text/css" href="/public/{{.}}"> {{end}} {{range .moreScripts}} <script src="/public/{{.}}" type="text/javascript" charset="utf-8"></script> {{end}} </head> <body>

 

你可能感兴趣的:(Go)