Skip to content

Commit 3243c2d

Browse files
committed
Added Template Helpers Structure and functionality
Added helpers: css, js
1 parent 2c6e22d commit 3243c2d

File tree

8 files changed

+59
-12
lines changed

8 files changed

+59
-12
lines changed

.travis.yml

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ before_install:
99
- go get github.com/gavv/httpexpect
1010
- go get github.com/spf13/viper
1111
script:
12-
13-
1412
- go build server.go
1513
matrix:
1614
allow_failures:

libs/renderer.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package libs
33
import (
44
"github.com/labstack/echo"
55
"html/template"
6+
"github.com/mrlsd/echo-cms/libs/template_helpers"
67
"io"
78
)
89

@@ -16,7 +17,7 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Con
1617

1718
func SetRenderer(e *echo.Echo, path string) {
1819
t := &Template{
19-
templates: template.Must(template.ParseGlob(path)),
20+
templates: template.Must(template.New("main").Funcs(libs.Helpers()).ParseGlob(path)),
2021
}
2122
e.Renderer = t
2223
}

libs/template_helpers/html.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package libs
2+
3+
import ("fmt"
4+
"html/template"
5+
)
6+
7+
func CssHelper(args ...interface{}) template.HTML {
8+
var content string
9+
content = ""
10+
if len(args) > 0 {
11+
content = fmt.Sprintf("<link rel=\"stylesheet\" href=\"%s\">", args[0])
12+
}
13+
return template.HTML(content)
14+
}
15+
16+
func JsHelper(args ...interface{}) template.HTML {
17+
var content string
18+
content = ""
19+
if len(args) > 0 {
20+
content = fmt.Sprintf("<script src=\"%s\"></script>", args[0])
21+
}
22+
return template.HTML(content)
23+
}

libs/template_helpers/main.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package libs
2+
3+
import (
4+
"html/template"
5+
)
6+
7+
func Helpers() template.FuncMap {
8+
return template.FuncMap{
9+
"css": CssHelper,
10+
"js": JsHelper,
11+
}
12+
}

modules/backend/controllers/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,5 @@ func PostForm(c echo.Context) error {
8585
}
8686

8787
func GetLogin(c echo.Context) error {
88-
return c.Render(http.StatusOK, "admin/login", nil)
88+
return c.Render(http.StatusOK, "login/main", nil)
8989
}

modules/backend/views/login/main.html

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{ define "login/main" }}
2+
<h1>Login page</h1>
3+
{{ end }}

server.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package main
22

33
import (
44
"github.com/labstack/echo"
5-
"github.com/labstack/gommon/log"
5+
//"github.com/labstack/gommon/log"
66

7-
"github.com/labstack/echo/middleware"
7+
//"github.com/labstack/echo/middleware"
88
"github.com/mrlsd/echo-cms/config"
99
"github.com/mrlsd/echo-cms/libs"
1010
"github.com/mrlsd/echo-cms/modules/backend"
@@ -19,16 +19,16 @@ func main() {
1919
backend.UrlRules(e)
2020
e.Static("/", "static")
2121

22-
e.Debug = true
23-
e.Logger.SetLevel(log.DEBUG)
22+
//e.Debug = true
23+
//e.Logger.SetLevel(log.DEBUG)
2424
e.HTTPErrorHandler = libs.CustomHTTPErrorHandler
2525

26-
e.Pre(middleware.NonWWWRedirect())
27-
e.Use(middleware.Secure())
26+
//e.Pre(middleware.NonWWWRedirect())
27+
//e.Use(middleware.Secure())
2828
//e.Use(middleware.CSRF())
2929
//e.Use(middleware.Logger())
30-
e.Use(middleware.Recover())
31-
e.Use(middleware.BodyLimit("2M"))
30+
//e.Use(middleware.Recover())
31+
//e.Use(middleware.BodyLimit("2M"))
3232

3333
e.Logger.Fatal(e.Start(":3000"))
3434
}

test.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
rm -rf *.coverprofile
2+
i=0
3+
for Package in $(go list ./... | grep -v "vendor")
4+
do
5+
(( i = i + 1 ))
6+
go test -v -covermode=count -coverprofile=$i.coverprofile $Package
7+
done
8+
gocovmerge *.coverprofile > merged.coverprofile
9+
go tool cover -html=merged.coverprofile
10+
rm -rf *.coverprofile

0 commit comments

Comments
 (0)