Skip to content

Commit

Permalink
issue #26 separated templates
Browse files Browse the repository at this point in the history
  • Loading branch information
wmentor committed Jan 9, 2021
1 parent f05251d commit 7b91ce3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
9 changes: 5 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Context struct {
qw Query
statusCode int
errorHandler ErrorHandler
tt *tt.TT
}

func (c *Context) StandardError(code int) {
Expand Down Expand Up @@ -222,13 +223,13 @@ func (c *Context) SetCookie(cookie *http.Cookie) {

func (c *Context) Render(tmpl string, vars map[string]interface{}) {

v := tt.MakeVars()
v := c.tt.MakeVars()

for k, val := range vars {
v.Set(k, val)
}

if res, err := tt.Render(tmpl, v); err == nil {
if res, err := c.tt.Render(tmpl, v); err == nil {
c.Write(res)
} else {
if c.errorHandler != nil {
Expand All @@ -239,13 +240,13 @@ func (c *Context) Render(tmpl string, vars map[string]interface{}) {

func (c *Context) RenderStr(tmpl string, vars map[string]interface{}) {

v := tt.MakeVars()
v := c.tt.MakeVars()

for k, val := range vars {
v.Set(k, val)
}

if res, err := tt.RenderString(tmpl, v); err == nil {
if res, err := c.tt.RenderString(tmpl, v); err == nil {
c.Write(res)
} else {
if c.errorHandler != nil {
Expand Down
4 changes: 4 additions & 0 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ func RegMethod(method string, fn interface{}) {
func RegisterJsonRPC(url string) {
server.RegisterJsonRPC(url)
}

func LoadTemplates(dir string) {
server.LoadTemplates(dir)
}
6 changes: 2 additions & 4 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type router struct {
staticHandlers map[string]http.Handler
fileHandlers map[string]http.Handler
authCheck AuthCheck
tt *tt.TT
}

func (sr *router) optionsOrNotFound(c *Context) {
Expand Down Expand Up @@ -77,6 +78,7 @@ func (r *router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
req: req,
params: make(map[string]string),
errorHandler: r.errorHandler,
tt: r.tt,
}

defer func() {
Expand Down Expand Up @@ -207,7 +209,3 @@ func makeUid(rw http.ResponseWriter, req *http.Request) {

http.SetCookie(rw, cookie)
}

func LoadTemplates(dir string) {
tt.Open(dir)
}
6 changes: 6 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/wmentor/jrpc"
"github.com/wmentor/tt"
)

type Server struct {
Expand All @@ -29,6 +30,7 @@ func New() *Server {
staticHandlers: make(map[string]http.Handler),
fileHandlers: make(map[string]http.Handler),
authCheck: func(login string, passwd string) bool { return false },
tt: tt.New(),
}

s.jrpc = jrpc.New()
Expand Down Expand Up @@ -183,3 +185,7 @@ func (s *Server) RegisterJsonRPC(url string) {
})

}

func (s *Server) LoadTemplates(dir string) {
s.router.tt = tt.New(dir)
}

0 comments on commit 7b91ce3

Please sign in to comment.