update the style

This commit is contained in:
astaxie
2015-04-02 13:27:34 +08:00
parent 36805f1991
commit 08e17bb2b9

View File

@@ -6,253 +6,288 @@ We've already introduced the entire concept behind the Beego framework through e
Our blog's directory structure can be seen below: Our blog's directory structure can be seen below:
/main.go ```
/views: /main.go
/view.tpl /views:
/new.tpl /view.tpl
/layout.tpl /new.tpl
/index.tpl /layout.tpl
/edit.tpl /index.tpl
/models/model.go /edit.tpl
/controllers: /models/model.go
/index.go /controllers:
/view.go /index.go
/new.go /view.go
/delete.go /new.go
/edit.go /delete.go
/edit.go
```
## Blog routing ## Blog routing
Our blog's main routing rules are as follows: Our blog's main routing rules are as follows:
//Show blog Home ```
beego.RegisterController("/", &controllers.IndexController{}) //Show blog Home
//View blog details beego.RegisterController("/", &controllers.IndexController{})
beego.RegisterController("/view/: id([0-9]+)", &controllers.ViewController{}) //View blog details
//Create blog Bowen beego.RegisterController("/view/: id([0-9]+)", &controllers.ViewController{})
beego.RegisterController("/new", &controllers.NewController{}) //Create blog Bowen
//Delete Bowen beego.RegisterController("/new", &controllers.NewController{})
beego.RegisterController("/delete/: id([0-9]+)", &controllers.DeleteController{}) //Delete Bowen
//Edit Bowen beego.RegisterController("/delete/: id([0-9]+)", &controllers.DeleteController{})
beego.RegisterController("/edit/: id([0-9]+)", &controllers.EditController{}) //Edit Bowen
beego.RegisterController("/edit/: id([0-9]+)", &controllers.EditController{})
```
## Database structure ## Database structure
A trivial database table to store basic blog information: A trivial database table to store basic blog information:
CREATE TABLE entries ( ```
id INT AUTO_INCREMENT, CREATE TABLE entries (
title TEXT, id INT AUTO_INCREMENT,
content TEXT, title TEXT,
created DATETIME, content TEXT,
primary key (id) created DATETIME,
); primary key (id)
);
```
## Controller ## Controller
IndexController: IndexController:
type IndexController struct { ```
beego.Controller type IndexController struct {
} beego.Controller
}
func (this *IndexController) Get() {
this.Data["blogs"] = models.GetAll() func (this *IndexController) Get() {
this.Layout = "layout.tpl" this.Data["blogs"] = models.GetAll()
this.TplNames = "index.tpl" this.Layout = "layout.tpl"
} this.TplNames = "index.tpl"
}
```
ViewController: ViewController:
type ViewController struct { ```
beego.Controller type ViewController struct {
} beego.Controller
}
func (this *ViewController) Get() {
inputs := this.Input() func (this *ViewController) Get() {
id, _ := strconv.Atoi(this.Ctx.Params[":id"]) inputs := this.Input()
this.Data["Post"] = models.GetBlog(id) id, _ := strconv.Atoi(this.Ctx.Params[":id"])
this.Layout = "layout.tpl" this.Data["Post"] = models.GetBlog(id)
this.TplNames = "view.tpl" this.Layout = "layout.tpl"
} this.TplNames = "view.tpl"
}
```
NewController NewController
type NewController struct {
beego.Controller ```
} type NewController struct {
beego.Controller
}
func (this *NewController) Get() {
this.Layout = "layout.tpl"
this.TplNames = "new.tpl"
}
func (this *NewController) Post() {
inputs := this.Input()
var blog models.Blog
blog.Title = inputs.Get("title")
blog.Content = inputs.Get("content")
blog.Created = time.Now()
models.SaveBlog(blog)
this.Ctx.Redirect(302, "/")
}
```
func (this *NewController) Get() {
this.Layout = "layout.tpl"
this.TplNames = "new.tpl"
}
func (this *NewController) Post() {
inputs := this.Input()
var blog models.Blog
blog.Title = inputs.Get("title")
blog.Content = inputs.Get("content")
blog.Created = time.Now()
models.SaveBlog(blog)
this.Ctx.Redirect(302, "/")
}
EditController EditController
type EditController struct { ```
beego.Controller type EditController struct {
} beego.Controller
}
func (this *EditController) Get() {
inputs := this.Input() func (this *EditController) Get() {
id, _ := strconv.Atoi(this.Ctx.Params[":id"]) inputs := this.Input()
this.Data["Post"] = models.GetBlog(id) id, _ := strconv.Atoi(this.Ctx.Params[":id"])
this.Layout = "layout.tpl" this.Data["Post"] = models.GetBlog(id)
this.TplNames = "new.tpl" this.Layout = "layout.tpl"
} this.TplNames = "new.tpl"
}
func (this *EditController) Post() {
inputs := this.Input() func (this *EditController) Post() {
var blog models.Blog inputs := this.Input()
blog.Id, _ = strconv.Atoi(inputs.Get("id")) var blog models.Blog
blog.Title = inputs.Get("title") blog.Id, _ = strconv.Atoi(inputs.Get("id"))
blog.Content = inputs.Get("content") blog.Title = inputs.Get("title")
blog.Created = time.Now() blog.Content = inputs.Get("content")
models.SaveBlog(blog) blog.Created = time.Now()
this.Ctx.Redirect(302, "/") models.SaveBlog(blog)
} this.Ctx.Redirect(302, "/")
}
```
DeleteController DeleteController
type DeleteController struct { ```
beego.Controller type DeleteController struct {
} beego.Controller
}
func (this *DeleteController) Get() {
id, _ := strconv.Atoi(this.Ctx.Params[":id"]) func (this *DeleteController) Get() {
this.Data["Post"] = models.DelBlog(id) id, _ := strconv.Atoi(this.Ctx.Params[":id"])
this.Ctx.Redirect(302, "/") this.Data["Post"] = models.DelBlog(id)
} this.Ctx.Redirect(302, "/")
}
```
## Model layer ## Model layer
package models ```
package models
import (
"database/sql" import (
"github.com/astaxie/beedb" "database/sql"
_ "github.com/ziutek/mymysql/godrv" "github.com/astaxie/beedb"
"time" _ "github.com/ziutek/mymysql/godrv"
) "time"
)
type Blog struct {
Id int `PK` type Blog struct {
Title string Id int `PK`
Content string Title string
Created time.Time Content string
} Created time.Time
}
func GetLink() beedb.Model {
db, err := sql.Open("mymysql", "blog/astaxie/123456") func GetLink() beedb.Model {
if err != nil { db, err := sql.Open("mymysql", "blog/astaxie/123456")
panic(err) if err != nil {
} panic(err)
orm := beedb.New(db)
return orm
}
func GetAll() (blogs []Blog) {
db := GetLink()
db.FindAll(&blogs)
return
}
func GetBlog(id int) (blog Blog) {
db := GetLink()
db.Where("id=?", id).Find(&blog)
return
}
func SaveBlog(blog Blog) (bg Blog) {
db := GetLink()
db.Save(&blog)
return bg
}
func DelBlog(blog Blog) {
db := GetLink()
db.Delete(&blog)
return
} }
orm := beedb.New(db)
return orm
}
func GetAll() (blogs []Blog) {
db := GetLink()
db.FindAll(&blogs)
return
}
func GetBlog(id int) (blog Blog) {
db := GetLink()
db.Where("id=?", id).Find(&blog)
return
}
func SaveBlog(blog Blog) (bg Blog) {
db := GetLink()
db.Save(&blog)
return bg
}
func DelBlog(blog Blog) {
db := GetLink()
db.Delete(&blog)
return
}
```
## View layer ## View layer
layout.tpl layout.tpl
<html> ```
<head> <html>
<title>My Blog</title> <head>
<style> <title>My Blog</title>
#menu { <style>
width: 200px; #menu {
float: right; width: 200px;
} float: right;
</style> }
</head> </style>
<body> </head>
<body>
<ul id="menu">
<li><a href="/">Home</a></li> <ul id="menu">
<li><a href="/new">New Post</a></li> <li><a href="/">Home</a></li>
</ul> <li><a href="/new">New Post</a></li>
</ul>
{{.LayoutContent}}
{{.LayoutContent}}
</body>
</html> </body>
</html>
```
index.tpl index.tpl
<h1>Blog posts</h1> ```
<h1>Blog posts</h1>
<ul> <ul>
{{range .blogs}} {{range .blogs}}
<li> <li>
<a href="/view/{{.Id}}">{{.Title}}</a> <a href="/view/{{.Id}}">{{.Title}}</a>
from {{.Created}} from {{.Created}}
<a href="/edit/{{.Id}}">Edit</a> <a href="/edit/{{.Id}}">Edit</a>
<a href="/delete/{{.Id}}">Delete</a> <a href="/delete/{{.Id}}">Delete</a>
</li> </li>
{{end}} {{end}}
</ul> </ul>
```
view.tpl view.tpl
<h1>{{.Post.Title}}</h1> ```
{{.Post.Created}}<br/> <h1>{{.Post.Title}}</h1>
{{.Post.Created}}<br/>
{{.Post.Content}}
{{.Post.Content}}
```
new.tpl new.tpl
<h1>New Blog Post</h1> ```
<form action="" method="post"> <h1>New Blog Post</h1>
Title:<input type="text" name="title"><br> <form action="" method="post">
Content<textarea name="content" colspan="3" rowspan="10"></textarea> Title:<input type="text" name="title"><br>
<input type="submit"> Content<textarea name="content" colspan="3" rowspan="10"></textarea>
</form> <input type="submit">
</form>
```
edit.tpl edit.tpl
<h1>Edit {{.Post.Title}}</h1> ```
<h1>Edit {{.Post.Title}}</h1>
<h1>New Blog Post</h1> <h1>New Blog Post</h1>
<form action="" method="post"> <form action="" method="post">
Title:<input type="text" name="title" value="{{.Post.Title}}"><br> Title:<input type="text" name="title" value="{{.Post.Title}}"><br>
Content<textarea name="content" colspan="3" rowspan="10">{{.Post.Content}}</textarea> Content<textarea name="content" colspan="3" rowspan="10">{{.Post.Content}}</textarea>
<input type="hidden" name="id" value="{{.Post.Id}}"> <input type="hidden" name="id" value="{{.Post.Id}}">
<input type="submit"> <input type="submit">
</form> </form>
```
## Links ## Links