Esempio n. 1
0
File: amber.go Progetto: Carbyn/iris
// BuildTemplates builds the amber templates
func (e *Engine) BuildTemplates() error {
	opt := amber.DirOptions{}
	opt.Recursive = true
	if e.Config.Extensions == nil || len(e.Config.Extensions) == 0 {
		e.Config.Extensions = []string{".html"}
	}

	// prepare the global amber funcs
	funcs := template.FuncMap{}
	for k, v := range amber.FuncMap { // add the amber's default funcs
		funcs[k] = v
	}
	if e.Config.Amber.Funcs != nil { // add the config's funcs
		for k, v := range e.Config.Amber.Funcs {
			funcs[k] = v
		}
	}

	amber.FuncMap = funcs //set the funcs

	opt.Ext = e.Config.Extensions[0]
	templates, err := amber.CompileDir(e.Config.Directory, opt, amber.DefaultOptions) // this returns the map with stripped extension, we want extension so we copy the map
	if err == nil {
		e.templateCache = make(map[string]*template.Template)
		for k, v := range templates {
			name := filepath.ToSlash(k + opt.Ext)
			e.templateCache[name] = v
			delete(templates, k)
		}

	}
	return err

}
Esempio n. 2
0
func compileAmberFiles(opt Options) (map[string]*template.Template, error) {
	ret, err := amber.CompileDir(opt.Directory, amber.DefaultDirOptions, amber.DefaultOptions)
	if os.IsNotExist(err) {
		return nil, nil
	}

	return ret, err
}
Esempio n. 3
0
func TestRenderAmberTemplate(t *testing.T) {
	dir := "fixtures/amber/"
	render := New(Options{})

	templates, err := amber.CompileDir(dir, amber.DefaultDirOptions, amber.DefaultOptions)
	if err != nil {
		t.Errorf("Could not compile Amber templates at " + dir)
	}

	a := Amber{
		Head: Head{
			ContentType: ContentHTML,
			Status:      http.StatusOK,
		},
		Template: templates["example"],
	}

	v := struct {
		VarOne string
		VarTwo string
	}{
		"Contact",
		"Content!",
	}

	h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		render.Render(w, a, v)
	})

	res := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/foo", nil)
	h.ServeHTTP(res, req)

	body := res.Body.String()

	checkCompile := strings.Index(body, `<div id="header">`) != -1
	checkVarOne := strings.Index(body, `<li>Contact</li>`) != -1
	checkVarTwo := strings.Index(body, `<li>Content!</li>`) != -1

	expect(t, checkCompile && checkVarOne && checkVarTwo, true)
	expect(t, res.Code, 200)
	expect(t, res.Header().Get(ContentType), ContentHTML)
}
Esempio n. 4
0
//initialization of the template map
func init() {
	templateMap, _ = amber.CompileDir("views",
		amber.DirOptions{Ext: ".amber", Recursive: true},
		amber.Options{PrettyPrint: false, LineNumbers: false})
}