diff --git a/beego.go b/beego.go
index 9c2eeb3c..36158a03 100644
--- a/beego.go
+++ b/beego.go
@@ -153,7 +153,7 @@ func (app *App) Run() {
}
}
-func (app *App) RegisterController(path string, c ControllerInterface) *App {
+func (app *App) Router(path string, c ControllerInterface) *App {
app.Handlers.Add(path, c)
return app
}
@@ -192,7 +192,12 @@ func (app *App) AccessLog(ctx *Context) {
}
func RegisterController(path string, c ControllerInterface) *App {
- BeeApp.RegisterController(path, c)
+ BeeApp.Router(path, c)
+ return BeeApp
+}
+
+func Router(path string, c ControllerInterface) *App {
+ BeeApp.Router(path, c)
return BeeApp
}
@@ -213,8 +218,8 @@ func FilterPrefixPath(path string, filter http.HandlerFunc) *App {
func Run() {
if PprofOn {
- BeeApp.RegisterController(`/debug/pprof`, &ProfController{})
- BeeApp.RegisterController(`/debug/pprof/:pp([\w]+)`, &ProfController{})
+ BeeApp.Router(`/debug/pprof`, &ProfController{})
+ BeeApp.Router(`/debug/pprof/:pp([\w]+)`, &ProfController{})
}
if SessionOn {
GlobalSessions, _ = session.NewManager(SessionProvider, SessionName, SessionGCMaxLifetime)
diff --git a/controller.go b/controller.go
index f90af6ee..f2ef3c31 100644
--- a/controller.go
+++ b/controller.go
@@ -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)
}
diff --git a/template.go b/template.go
index 56c94a30..582b4779 100644
--- a/template.go
+++ b/template.go
@@ -5,14 +5,11 @@ package beego
import (
"errors"
"fmt"
- "github.com/russross/blackfriday"
"html/template"
"os"
"path"
"path/filepath"
- "regexp"
"strings"
- "time"
)
var (
@@ -32,110 +29,9 @@ func init() {
beegoTplFuncMap["compare"] = Compare
beegoTplFuncMap["substr"] = Substr
beegoTplFuncMap["html2str"] = Html2str
-}
-
-// MarkDown parses a string in MarkDown format and returns HTML. Used by the template parser as "markdown"
-func MarkDown(raw string) (output template.HTML) {
- input := []byte(raw)
- bOutput := blackfriday.MarkdownBasic(input)
- output = template.HTML(string(bOutput))
- return
-}
-
-func Substr(s string, start, length int) string {
- bt := []rune(s)
- if start < 0 {
- start = 0
- }
- var end int
- if (start + length) > (len(bt) - 1) {
- end = len(bt) - 1
- } else {
- end = start + length
- }
- return string(bt[start:end])
-}
-
-// Html2str() returns escaping text convert from html
-func Html2str(html string) string {
- src := string(html)
-
- //将HTML标签全转换成小写
- re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
- src = re.ReplaceAllStringFunc(src, strings.ToLower)
-
- //去除STYLE
- re, _ = regexp.Compile("\\")
+ src = re.ReplaceAllString(src, "")
+
+ //去除SCRIPT
+ re, _ = regexp.Compile("\\