From 8f29442ff40fd4d0fd450a9624dda610b28b3896 Mon Sep 17 00:00:00 2001 From: dtookey Date: Wed, 3 Aug 2022 11:08:25 -0400 Subject: [PATCH] tenative draft of template routes --- go.mod | 2 +- servlet/server.go | 2 +- servlet/staticRoute.go | 2 +- servlet/templateRoute.go | 38 ++++++++++++++++++++++++++++++++++++++ servlet/templates.go | 28 +++++++++++++++++++++++++++- 5 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 servlet/templateRoute.go diff --git a/go.mod b/go.mod index 9c470a1..b691494 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module vinegar +module geniuscartel.xyz/vinegar go 1.18 diff --git a/servlet/server.go b/servlet/server.go index 91f2a81..b956b08 100644 --- a/servlet/server.go +++ b/servlet/server.go @@ -1,13 +1,13 @@ package servlet import ( + "geniuscartel.xyz/vinegar/vinegarUtil" "html/template" "io/ioutil" "log" "net/http" "os" "regexp" - "vinegar/vinegarUtil" ) const ( diff --git a/servlet/staticRoute.go b/servlet/staticRoute.go index c29f737..b87bb50 100644 --- a/servlet/staticRoute.go +++ b/servlet/staticRoute.go @@ -1,10 +1,10 @@ package servlet import ( + util "geniuscartel.xyz/vinegar/vinegarUtil" "net/http" "path" "strings" - util "vinegar/vinegarUtil" ) type ( diff --git a/servlet/templateRoute.go b/servlet/templateRoute.go new file mode 100644 index 0000000..7234879 --- /dev/null +++ b/servlet/templateRoute.go @@ -0,0 +1,38 @@ +package servlet + +import ( + "net/http" + "strings" +) + +type ( + TemplateRoute struct { + *VinegarRoute + srv *VinegarServlet + fileRoot string + TemplateManager *TemplateManager + UseCache bool + } + TemplateRouteHandlerFunc func(w http.ResponseWriter, r *http.Request, tm *TemplateManager) +) + +func NewTemplateRoute(servlet *VinegarServlet, urlPattern string, templatePath string, componentPath string, handler TemplateRouteHandlerFunc) *TemplateRoute { + defaultPrune := strings.Replace(urlPattern, ".*", "", -1) + tm := NewTemplateManager(templatePath, componentPath) + rootRoute := NewServletRoute(defaultPrune, createTemplateRouteFunction(tm, handler)) + route := TemplateRoute{ + VinegarRoute: rootRoute, + srv: servlet, + fileRoot: "", + TemplateManager: tm, + UseCache: false, + } + return &route +} + +func createTemplateRouteFunction(tm *TemplateManager, handler TemplateRouteHandlerFunc) VinegarHandlerFunction { + fun := func(w http.ResponseWriter, r *http.Request) { + handler(w, r, tm) + } + return fun +} diff --git a/servlet/templates.go b/servlet/templates.go index 259d549..fcd9408 100644 --- a/servlet/templates.go +++ b/servlet/templates.go @@ -1,14 +1,15 @@ package servlet import ( + "errors" "fmt" + util "geniuscartel.xyz/vinegar/vinegarUtil" "html/template" "io/ioutil" "net/http" "os" "path" "strings" - util "vinegar/vinegarUtil" ) type ( @@ -17,6 +18,7 @@ type ( componentPath string templates map[string]*template.Template components map[string]template.HTML + mixins map[string]string Templates []string } ) @@ -91,6 +93,30 @@ func (tm *TemplateManager) GetComponent(key string) template.HTML { return content } +func (tm *TemplateManager) GetMixin(key string) template.HTML { + content, exists := tm.mixins[key] + if exists { + return template.HTML(content) + } else { + return "" + } +} + +func (tm *TemplateManager) AddMixin(key string, value string) error { + tm.mixins[key] = value + return nil +} + +func (tm *TemplateManager) AddMixinFromFile(key string, pathlike string) error { + content, exists := util.GetDiskContent(pathlike) + if exists { + tm.AddMixin(key, string(*content)) + return nil + } else { + return errors.New("mixin source file not found at " + pathlike) + } +} + func (tm *TemplateManager) RenderAllToFile(pathlike string) { for _, v := range tm.Templates { path := path.Join(pathlike, v)