1
0
mirror of https://github.com/astaxie/beego.git synced 2025-07-12 05:01:02 +00:00

add some useful function

1. Router
2. SetSession/GetSession/DelSession
3. RenderString
4. str2html/htmlquote/htmlunquote
5. move the template Function to util.go
This commit is contained in:
astaxie
2013-04-03 23:37:59 +08:00
parent 74d8952c5a
commit 69f40ad18a
4 changed files with 197 additions and 131 deletions

View File

@ -82,6 +82,21 @@ func (c *Controller) Options() {
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
}
func (c *Controller) SetSession(name string, value interface{}) {
ss := c.StartSession()
ss.Set(name, value)
}
func (c *Controller) GetSession(name string) interface{} {
ss := c.StartSession()
return ss.Get(name)
}
func (c *Controller) DelSession(name string) {
ss := c.StartSession()
ss.Delete(name)
}
func (c *Controller) Render() error {
rb, err := c.RenderBytes()
@ -96,32 +111,29 @@ func (c *Controller) Render() error {
return nil
}
func (c *Controller) RenderString() (string, error) {
b, e := c.RenderBytes()
return string(b), e
}
func (c *Controller) RenderBytes() ([]byte, error) {
//if the controller has set layout, then first get the tplname's content set the content to the layout
var t *template.Template
var err error
if c.Layout != "" {
if c.TplNames == "" {
c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt
}
if RunMode == "dev" {
t, err = template.New("beegoTemplate").Funcs(beegoTplFuncMap).ParseFiles(path.Join(ViewsPath, c.TplNames), path.Join(ViewsPath, c.Layout))
if err != nil {
Trace("template ParseFiles err:", err)
}
} else {
subdir := path.Dir(c.TplNames)
t = BeeTemplates[subdir]
BuildTemplate(ViewsPath)
}
subdir := path.Dir(c.TplNames)
_, file := path.Split(c.TplNames)
newbytes := bytes.NewBufferString("")
t.ExecuteTemplate(newbytes, file, c.Data)
BeeTemplates[subdir].ExecuteTemplate(newbytes, file, c.Data)
tplcontent, _ := ioutil.ReadAll(newbytes)
c.Data["LayoutContent"] = template.HTML(string(tplcontent))
_, file = path.Split(c.Layout)
ibytes := bytes.NewBufferString("")
err := t.ExecuteTemplate(ibytes, file, c.Data)
err := BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
if err != nil {
Trace("template Execute err:", err)
}
@ -132,17 +144,12 @@ func (c *Controller) RenderBytes() ([]byte, error) {
c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt
}
if RunMode == "dev" {
t, err = template.New("beegoTemplate").Funcs(beegoTplFuncMap).ParseFiles(path.Join(ViewsPath, c.TplNames))
if err != nil {
Trace("template ParseFiles err:", err)
}
} else {
subdir := path.Dir(c.TplNames)
t = BeeTemplates[subdir]
BuildTemplate(ViewsPath)
}
subdir := path.Dir(c.TplNames)
_, file := path.Split(c.TplNames)
ibytes := bytes.NewBufferString("")
err := t.ExecuteTemplate(ibytes, file, c.Data)
err := BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
if err != nil {
Trace("template Execute err:", err)
}