Esempio n. 1
0
func LoadDir(dir string) int {
	numTemplates := 0
	walker := func(path string, info os.FileInfo, err error) error {
		name, isDir := info.Name(), info.IsDir()
		if isDir {
			return nil
		}
		if name[0] == '.' {
			return nil
		}
		tname := strings.Join(strings.Split(path, "/")[1:], "/")
		templatePaths[tname] = path
		// if template pre-compilation is on, compile these and store
		if conf.Config.TemplatePreCompile {
			templates[tname], err = mandira.ParseFile(path)
			if err != nil {
				fmt.Printf("%s: %v\n", tname, err)
			}
		}
		numTemplates++
		return nil
	}
	filepath.Walk(dir, walker)
	return numTemplates
}
Esempio n. 2
0
func Render(t string, c ...interface{}) string {
	// add the Config to all of our template rendering
	c = append(c, dict{"Debug": conf.Config.Debug})
	c = append(c, dict{"Config": conf.Config})

	if conf.Config.TemplatePreCompile {
		template := templates[t]
		if template == nil {
			fmt.Printf("Error: template %s not found\n", t)
			return ""
		}
		return template.Render(c...)
	}
	path := templatePaths[t]
	if len(path) < 0 {
		fmt.Printf("Error: template %s not found\n", t)
		return ""
	}
	template, err := mandira.ParseFile(path)
	if err != nil {
		fmt.Println(err)
		return ""
	}
	return template.Render(c...)
}
Esempio n. 3
0
func MustParse(path string) *mandira.Template {
	var t *mandira.Template
	var err error
	if len(path) > 40 {
		t, err = mandira.ParseString(path)
	} else {
		t, err = mandira.ParseFile(path)
	}
	if err != nil {
		log.Fatal(err)
	}
	return t
}