transmet/templates.go

98 lines
2.5 KiB
Go
Raw Permalink Normal View History

package main
import (
2015-09-13 23:57:22 +02:00
"errors"
"fmt"
"github.com/dballard/transmet/categories"
"github.com/gorilla/csrf"
"html/template"
2015-09-13 23:57:22 +02:00
"net/http"
"path/filepath"
"regexp"
"strings"
2015-09-13 23:57:22 +02:00
"time"
)
var (
2015-09-13 23:57:22 +02:00
templates = map[string]*template.Template{}
)
2015-05-23 19:49:27 +02:00
// template helper function
2015-09-13 23:57:22 +02:00
func dict(values ...interface{}) (map[string]interface{}, error) {
2015-05-23 19:49:27 +02:00
if len(values)%2 != 0 {
2015-09-13 23:57:22 +02:00
return nil, errors.New("invalid dict call")
}
dict := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
2015-05-23 19:49:27 +02:00
}
// string multiplication
// stringTimes(3, "Foo") => "FooFooFoo"
2015-05-23 19:49:27 +02:00
func stringTimes(times int, str string) string {
2015-09-13 23:57:22 +02:00
result := ""
for i := 0; i < times; i++ {
result += str
}
return result
}
2015-05-23 19:49:27 +02:00
// Turns a Time into a formated string
func dateFormat(t time.Time) string {
2015-09-13 23:57:22 +02:00
return t.Format("2006.01.02 15:04:05")
}
// takes a category_id and returns "Root / Parent / Category"
func fullCategoryPath(categoriesFlat map[int]*categories.Category, category_id int) string {
2015-09-13 23:57:22 +02:00
var categoryNames []string = nil
for category := categoriesFlat[category_id]; category != nil; category = categoriesFlat[int(category.Parent.Int64)] {
categoryNames = append([]string{category.Name}, categoryNames...)
}
return strings.Join(categoryNames, " / ")
}
2015-08-31 05:23:35 +02:00
// truncate a string
func truncate(str string, maxLen int) string {
2015-09-13 23:57:22 +02:00
if len(str) <= maxLen {
return str
}
return str[0:maxLen] + "..."
2015-08-31 05:23:35 +02:00
}
// Tempalte helper functions
2015-09-13 23:57:22 +02:00
var funcMap = template.FuncMap{
"add": func(x, y int) int { return x + y },
"minus": func(x, y int) int { return x - y },
"dict": dict,
"stringTimes": stringTimes,
"dateFormat": dateFormat,
"fullCategoryPath": fullCategoryPath,
"truncate": truncate,
}
func initTemplates() {
files, _ := filepath.Glob("templates/pages/*.html")
re := regexp.MustCompile("templates/pages/(.*).html")
fmt.Println("Loading Templates:")
for _, t := range files {
name := re.FindStringSubmatch(t)
fmt.Println(" ", name[1])
templates[name[1]] = template.Must(template.New(name[1]).Funcs(funcMap).ParseFiles("templates/layout.html", t))
}
}
func ShowTemplate(template string, w http.ResponseWriter, r *http.Request, data map[string]interface{}) {
data[csrf.TemplateTag] = csrf.TemplateField(r)
err := templates[template].ExecuteTemplate(w, "layout.html", data)
if err != nil {
fmt.Println("Exec err: ", err)
}
// TODO: show error 500 page
2015-09-13 23:57:22 +02:00
}