tenative draft of template routes
This commit is contained in:
parent
03a454bf51
commit
8f29442ff4
@ -1,13 +1,13 @@
|
|||||||
package servlet
|
package servlet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"geniuscartel.xyz/vinegar/vinegarUtil"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"vinegar/vinegarUtil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
package servlet
|
package servlet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
util "geniuscartel.xyz/vinegar/vinegarUtil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
util "vinegar/vinegarUtil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
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
|
package servlet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
util "geniuscartel.xyz/vinegar/vinegarUtil"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
util "vinegar/vinegarUtil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -17,6 +18,7 @@ type (
|
|||||||
componentPath string
|
componentPath string
|
||||||
templates map[string]*template.Template
|
templates map[string]*template.Template
|
||||||
components map[string]template.HTML
|
components map[string]template.HTML
|
||||||
|
mixins map[string]string
|
||||||
Templates []string
|
Templates []string
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -91,6 +93,30 @@ func (tm *TemplateManager) GetComponent(key string) template.HTML {
|
|||||||
return content
|
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) {
|
func (tm *TemplateManager) RenderAllToFile(pathlike string) {
|
||||||
for _, v := range tm.Templates {
|
for _, v := range tm.Templates {
|
||||||
path := path.Join(pathlike, v)
|
path := path.Join(pathlike, v)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user