now reads all .gtpl files in ./templates folder

This commit is contained in:
thewhitetulip 2015-11-17 22:02:14 +05:30
parent 843ad5c050
commit 6320ceeb6b
1 changed files with 18 additions and 16 deletions

32
main.go
View File

@ -8,9 +8,11 @@ import (
"fmt" "fmt"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"github.com/thewhitetulip/Tasks/viewmodels" "github.com/thewhitetulip/Tasks/viewmodels"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"text/template" "text/template"
) )
@ -19,31 +21,31 @@ var deletedTemplate *template.Template
var completedTemplate *template.Template var completedTemplate *template.Template
var editTemplate *template.Template var editTemplate *template.Template
var searchTemplate *template.Template var searchTemplate *template.Template
var templates *template.Template
var err error var err error
func main() { func main() {
defer viewmodels.Close() defer viewmodels.Close()
homeTemplate, err = template.ParseFiles("./templates/home.gtpl") var allFiles []string
if err != nil { files, err := ioutil.ReadDir("./templates")
fmt.Println(err) for _, file := range files {
filename := file.Name()
if strings.HasSuffix(filename, ".gtpl") {
allFiles = append(allFiles, "./templates/"+filename)
} }
deletedTemplate, err = template.ParseFiles("./templates/deleted.gtpl")
if err != nil {
fmt.Println(err)
} }
editTemplate, err = template.ParseFiles("./templates/edit.gtpl")
if err != nil {
fmt.Println(err)
}
searchTemplate, err = template.ParseFiles("./templates/search.gtpl")
if err != nil {
fmt.Println(err)
}
completedTemplate, err = template.ParseFiles("./templates/completed.gtpl")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
templates, err = template.ParseFiles(allFiles...)
fmt.Println(templates)
homeTemplate = templates.Lookup("home.gtpl")
deletedTemplate = templates.Lookup("deleted.gtpl")
editTemplate = templates.Lookup("edit.gtpl")
searchTemplate = templates.Lookup("search.gtpl")
completedTemplate = templates.Lookup("completed.gtpl")
router := httprouter.New() router := httprouter.New()
router.GET("/", ShowAllTasks) router.GET("/", ShowAllTasks)