Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

layout based templates

goenning85c38d5ffdae
diff --git a/handler/index_test.go b/handler/index_test.go
index acfcbb91b..8a545cdee 100644
--- a/handler/index_test.go
+++ b/handler/index_test.go
@@ -1,14 +1,11 @@
package handler_test
import (
- "html/template"
"net/http"
"net/http/httptest"
- "os"
"testing"
"github.com/WeCanHearYou/wchy/context"
- "github.com/WeCanHearYou/wchy/env"
"github.com/WeCanHearYou/wchy/handler"
"github.com/WeCanHearYou/wchy/model"
"github.com/WeCanHearYou/wchy/router"
@@ -30,15 +27,7 @@ func TestIndexHandler(t *testing.T) {
}
e := echo.New()
-
- path := "views/*.html"
- if env.IsTest() {
- path = os.Getenv("GOPATH") + "/src/github.com/WeCanHearYou/wchy/" + path
- }
-
- e.Renderer = &router.HTMLRenderer{
- Templates: template.Must(template.ParseGlob(path)),
- }
+ e.Renderer = router.NewHTMLRenderer()
req, _ := http.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
diff --git a/router/renderer.go b/router/renderer.go
index bb3ff9e4e..a8b9cbde0 100644
--- a/router/renderer.go
+++ b/router/renderer.go
@@ -1,18 +1,53 @@
package router
import (
+ "fmt"
"html/template"
"io"
+ "log"
+ "os"
+
+ "github.com/WeCanHearYou/wchy/env"
"github.com/labstack/echo"
)
//HTMLRenderer renderer
type HTMLRenderer struct {
- Templates *template.Template
+ templates map[string]*template.Template
+}
+
+// NewHTMLRenderer creates a new HTMLRenderer
+func NewHTMLRenderer() *HTMLRenderer {
+ renderer := &HTMLRenderer{}
+ renderer.templates = make(map[string]*template.Template)
+
+ path := "views/"
+ if env.IsTest() {
+ path = os.Getenv("GOPATH") + "/src/github.com/WeCanHearYou/wchy/" + path
+ }
+
+ renderer.add(path, "index.html")
+
+ return renderer
}
//Render a template based on parameters
-func (t *HTMLRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
- return t.Templates.ExecuteTemplate(w, name, data)
+func (r *HTMLRenderer) add(path, name string) {
+ tpl, err := template.ParseFiles(path+"base.html", path+name)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ r.templates[name] = tpl
+}
+
+//Render a template based on parameters
+func (r *HTMLRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
+ tmpl, ok := r.templates[name]
+ if !ok {
+ return fmt.Errorf("The template %s does not exist", name)
+ }
+
+ return tmpl.Execute(w, data)
}
diff --git a/router/router.go b/router/router.go
index 1716c66ca..22f8a6b27 100644
--- a/router/router.go
+++ b/router/router.go
@@ -1,16 +1,11 @@
package router
import (
- "html/template"
-
- "os"
-
"fmt"
"net/http"
"github.com/WeCanHearYou/wchy/context"
- "github.com/WeCanHearYou/wchy/env"
"github.com/WeCanHearYou/wchy/handler"
"github.com/labstack/echo"
)
@@ -19,14 +14,7 @@ import (
func GetMainEngine(ctx *context.WchyContext) *echo.Echo {
router := echo.New()
- path := "views/*.html"
- if env.IsTest() {
- path = os.Getenv("GOPATH") + "/src/github.com/WeCanHearYou/wchy/" + path
- }
-
- router.Renderer = &HTMLRenderer{
- Templates: template.Must(template.ParseGlob(path)),
- }
+ router.Renderer = NewHTMLRenderer()
router.HTTPErrorHandler = func(e error, c echo.Context) {
fmt.Println(e)
diff --git a/views/base.html b/views/base.html
new file mode 100644
index 000000000..c2d8e961c
--- /dev/null
+++ b/views/base.html
@@ -0,0 +1,59 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8"/>
+ <meta name="viewport" content="width=device-width,initial-scale=1">
+ <link rel="stylesheet" type="text/css" href="/assets/semantic-ui-css/semantic.min.css">
+ <title>{{template "title" .}}</title>
+ <style>
+ .ideas-list .vote-col {
+ text-align: center;
+ width: 140px;
+ }
+ .ideas-list .vote-col div {
+ display: block;
+ }
+ #footer {
+ margin-top: 50px;
+ }
+ </style>
+</head>
+<body>
+ {{template "content" .}}
+ <div id="footer" class="ui inverted vertical footer segment">
+ <div class="ui center aligned container">
+ <div class="ui stackable inverted divided grid">
+ <div class="six wide column">
+ <h4 class="ui inverted header">Trending ideas</h4>
+ <div class="ui inverted link list">
+ <a href="#" class="item">Link #1</a>
+ <a href="#" class="item">Link #2</a>
+ <a href="#" class="item">Link #3</a>
+ <a href="#" class="item">Link $3</a>
+ </div>
+ </div>
+ <div class="six wide column">
+ <h4 class="ui inverted header">Latest activity</h4>
+ <div class="ui inverted link list">
+ <a href="#" class="item">Someone did something</a>
+ <a href="#" class="item">New idea was posted</a>
+ <a href="#" class="item">Someone commented on idea #1</a>
+ <a href="#" class="item">Idea #2 changed to Shipped</a>
+ </div>
+ </div>
+ <div class="four wide column">
+ <h4 class="ui inverted header">Footer Header</h4>
+ <p>Extra space for a call to action inside the footer that could help re-engage users.</p>
+ </div>
+ </div>
+ <div class="ui inverted section divider"></div>
+ <div class="ui horizontal inverted small divided link list">
+ <a class="item" href="#">Site Map</a>
+ <a class="item" href="#">Contact Us</a>
+ <a class="item" href="#">Terms and Conditions</a>
+ <a class="item" href="#">Privacy Policy</a>
+ </div>
+ </div>
+ </div>
+</body>
+</html>
diff --git a/views/index.html b/views/index.html
index 45545c58e..b6754108e 100644
--- a/views/index.html
+++ b/views/index.html
@@ -1,24 +1,8 @@
-<!DOCTYPE html>
-<html>
-<head>
- <meta charset="utf-8"/>
- <meta name="viewport" content="width=device-width,initial-scale=1">
- <link rel="stylesheet" type="text/css" href="/assets/semantic-ui-css/semantic.min.css">
- <title>{{block "title" .}}{{end}}</title>
- <style>
- .ideas-list .vote-col {
- text-align: center;
- width: 140px;
- }
- .ideas-list .vote-col div {
- display: block;
- }
- #footer {
- margin-top: 50px;
- }
- </style>
-</head>
-<body>
+{{define "title"}}
+ {{ .Tenant.Name }}
+{{end}}
+
+{{define "content"}}
<div class="ui menu">
<div class="ui container">
<div class="header item">
@@ -74,41 +58,4 @@
</div>
</div>
-
- <div id="footer" class="ui inverted vertical footer segment">
- <div class="ui center aligned container">
- <div class="ui stackable inverted divided grid">
- <div class="six wide column">
- <h4 class="ui inverted header">Trending ideas</h4>
- <div class="ui inverted link list">
- <a href="#" class="item">Link #1</a>
- <a href="#" class="item">Link #2</a>
- <a href="#" class="item">Link #3</a>
- <a href="#" class="item">Link $3</a>
- </div>
- </div>
- <div class="six wide column">
- <h4 class="ui inverted header">Latest activity</h4>
- <div class="ui inverted link list">
- <a href="#" class="item">Someone did something</a>
- <a href="#" class="item">New idea was posted</a>
- <a href="#" class="item">Someone commented on idea #1</a>
- <a href="#" class="item">Idea #2 changed to Shipped</a>
- </div>
- </div>
- <div class="four wide column">
- <h4 class="ui inverted header">Footer Header</h4>
- <p>Extra space for a call to action inside the footer that could help re-engage users.</p>
- </div>
- </div>
- <div class="ui inverted section divider"></div>
- <div class="ui horizontal inverted small divided link list">
- <a class="item" href="#">Site Map</a>
- <a class="item" href="#">Contact Us</a>
- <a class="item" href="#">Terms and Conditions</a>
- <a class="item" href="#">Privacy Policy</a>
- </div>
- </div>
- </div>
-</body>
-</html>
+{{end}}
\ No newline at end of file