tenative draft of template routes
This commit is contained in:
parent
03a454bf51
commit
8f29442ff4
@ -1,13 +1,13 @@
|
||||
package servlet
|
||||
|
||||
import (
|
||||
"geniuscartel.xyz/vinegar/vinegarUtil"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"vinegar/vinegarUtil"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
package servlet
|
||||
|
||||
import (
|
||||
util "geniuscartel.xyz/vinegar/vinegarUtil"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
util "vinegar/vinegarUtil"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
38
servlet/templateRoute.go
Normal file
38
servlet/templateRoute.go
Normal file
@ -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
|
||||
}
|
||||
@ -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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user