Răsfoiți Sursa

Working on template funcs

Andrea Fazzi 5 ani în urmă
părinte
comite
24839b55d4

+ 0 - 2
orm/teacher.go

@@ -2,7 +2,6 @@ package orm
 
 import (
 	"fmt"
-	"log"
 	"net/http"
 	"strconv"
 	"time"
@@ -72,7 +71,6 @@ func (t *Teacher) String() string {
 
 func (t *Teacher) Create(args map[string]string, r *http.Request) (interface{}, error) {
 	if r.Method == "GET" {
-		log.Println("IN CREATE!!!")
 		return nil, nil
 	} else {
 		teacher := new(Teacher)

+ 37 - 0
renderer/funcmap.go

@@ -0,0 +1,37 @@
+package renderer
+
+import (
+	"fmt"
+	"html/template"
+	"net/url"
+	"reflect"
+	"time"
+)
+
+var (
+	funcMap = template.FuncMap{
+		"query":       query,
+		"convertDate": convertDate,
+		"model":       model,
+	}
+)
+
+func query(values ...string) template.URL {
+	var urlValues url.Values
+
+	urlValues = make(url.Values)
+	urlValues.Set("format", "html")
+
+	for i := 0; i < len(values); i += 2 {
+		urlValues.Add(values[i], values[i+1])
+	}
+	return template.URL(urlValues.Encode())
+}
+
+func convertDate(t time.Time) string {
+	return fmt.Sprintf("%d-%02d-%02d", t.Year(), t.Month(), t.Day())
+}
+
+func model(value interface{}) string {
+	return reflect.ValueOf(model).String()
+}

+ 0 - 21
renderer/renderer.go

@@ -42,11 +42,6 @@ var (
 	currRenderer Renderer
 	Render       map[string]func(http.ResponseWriter, *http.Request, interface{}, ...url.Values)
 
-	funcMap = template.FuncMap{
-		"query":       query,
-		"convertDate": convertDate,
-	}
-
 	contentTypeToFormat = map[string]string{
 		"application/x-www-form-urlencoded": "html",
 		"application/json":                  "json",
@@ -100,22 +95,6 @@ func Query(values ...string) template.URL {
 	return query(values...)
 }
 
-func query(values ...string) template.URL {
-	var urlValues url.Values
-
-	urlValues = make(url.Values)
-	urlValues.Set("format", "html")
-
-	for i := 0; i < len(values); i += 2 {
-		urlValues.Add(values[i], values[i+1])
-	}
-	return template.URL(urlValues.Encode())
-}
-
-func convertDate(t time.Time) string {
-	return fmt.Sprintf("%d-%02d-%02d", t.Year(), t.Month(), t.Day())
-}
-
 // FIXME: Not safe. We should check if it responds to the error interface instead.
 func isErrorType(data interface{}) bool {
 	return strings.Contains(strings.ToLower(reflect.TypeOf(data).String()), "error")

+ 6 - 0
templates/layout/button.html.tpl

@@ -0,0 +1,6 @@
+{{define "button"}}
+<a href="/teachers/create/?{{query "tpl_layout" "base" "tpl_content" "teachers_add_update"}}" class="btn btn-primary">
+  <span class="fa fa-plus-circle" aria-hidden="true"></span>
+  Crea nuovo {{. | model}}
+</a>
+{{end}}

+ 1 - 6
templates/teachers.html.tpl

@@ -10,12 +10,7 @@
       <div class="col-md-4">
 	
 	<div class="btn-group float-right">
-	  
-	  <a href="/teachers/create/?{{query "tpl_layout" "base" "tpl_content" "teachers_add_update"}}" class="btn btn-primary">
-	    <span class="fa fa-plus-circle" aria-hidden="true"></span>
-	    Crea nuovo docente
-	  </a>
-	   	
+	  {{template "button"}}
       </div>
     </div>
   </div>