Commits

Miki Tebeka  committed 8bf48f5

LoadTemplates

  • Participants
  • Parent commits 77cca73

Comments (0)

Files changed (4)

     This allows you more control on how to serve.
 
 
+`LoadTemplates(t *template.Template, filenames ...string) (*template.Template, error)`
+    Will load named templates from resources. If the argument "t" is `nil`, it is
+    created from the first resource.
+
 Resource Interface
 ------------------
 
 import (
 	"bytes"
 	"fmt"
+	"html/template"
 	"io"
+	"io/ioutil"
 	"net/http"
+	"path/filepath"
+	"strings"
 	"time"
 )
 
 	io.Copy(w, rsc.Open())
 }
 
+// Get returns the named resource (nil if not found)
 func Get(path string) Resource {
 	return resources[path]
 }
 
+// Handle register HTTP handler under prefix
 func Handle(prefix string) {
+	if !strings.HasSuffix(prefix, "/") {
+		prefix += "/"
+	}
 	var h handler
 	http.Handle(prefix, http.StripPrefix(prefix, h))
 }
+
+// LoadTemplates loads named templates from resources.
+// If the argument "t" is nil, it is created from the first resource.
+func LoadTemplates(t *template.Template, filenames ...string) (*template.Template, error) {
+	if len(filenames) == 0 {
+		// Not really a problem, but be consistent.
+		return nil, fmt.Errorf("no files named in call to LoadTemplates")
+	}
+
+	for _, filename := range filenames {
+		rsc := Get(filename)
+		if rsc == nil {
+			return nil, fmt.Errorf("can't find %s", filename)
+		}
+
+		data, err := ioutil.ReadAll(rsc.Open())
+		if err != nil {
+			return nil, err
+		}
+
+		var tmpl *template.Template
+		name := filepath.Base(filename)
+		if t == nil {
+			t = template.New(name)
+		}
+		if name == t.Name() {
+			tmpl = t
+		} else {
+			tmpl = t.New(name)
+		}
+		_, err = tmpl.Parse(string(data))
+		if err != nil {
+			return nil, err
+		}
+	}
+	return t, nil
+}
 `

File nrsc_test.go

 
 import (
 	"fmt"
+	"io/ioutil"
 	"net/http"
 	"os"
 	"os/exec"
 	checkPath(t, "sub/favicon.ico", expected)
 }
 
+// / serves a template
+func TestTempalte(t *testing.T) {
+	server := startServer(t)
+	if server == nil {
+		t.Fatalf("can't start server")
+	}
+	defer server.Process.Kill()
+
+	url := fmt.Sprintf("http://localhost:%d", port)
+	resp, err := http.Get(url)
+	if err != nil {
+		t.Fatalf("can't GET / - %s", err)
+	}
+
+	data, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		t.Fatalf("can't read body - %s", err)
+	}
+
+	if string(data) != "The number is 7\n" {
+		t.Fatalf("bad template reply - %s", string(data))
+	}
+}
+
 func createMain() error {
 	filename := fmt.Sprintf("%s/main.go", root)
 	file, err := os.Create(filename)
 	"./nrsc"
 )
 
+type params struct {
+	Number  int
+}
+
 func indexHandler(w http.ResponseWriter, req *http.Request) {
-	fmt.Fprintf(w, "Hello World\n")
+	t, err := nrsc.LoadTemplates(nil, "t.html")
+	if err != nil {
+		http.NotFound(w, req)
+	}
+	if err = t.Execute(w, params{7}); err != nil {
+		http.NotFound(w, req)
+	}
 }
 
 func main() {
 	nrsc.Handle("/static/")
 	http.HandleFunc("/", indexHandler)
-	if err := http.ListenAndServe(":%d", nil); err != nil {
-		fmt.Fprintf(os.Stderr, "error: %%s\n", err)
+	if err := http.ListenAndServe(":9888", nil); err != nil {
+		fmt.Fprintf(os.Stderr, "error: %s\n", err)
 		os.Exit(1)
 	}
 }

File test-resources/t.html

+The number is {{.Number}}