Example #1
0
// Loads any templates from the given directory into the given template.
func (this *Taxidermist) loadPath(path string) error {
	dir, err := os.Open(path)
	if os.IsNotExist(err) {
		// Do nothing. Ignore URL components that don't exist.
		return nil
	} else if err != nil {
		return errors.Stack(err)
	}

	fileInfos, err := dir.Readdir(0)
	if err != nil {
		return errors.Stack(err)
	}

	var templates []string

	for _, value := range this.template.Templates() {
		templates = append(templates, value.Name())
	}

	// Go over all the files in the directory, and add any 'html' files to the template.
	for _, fileInfo := range fileInfos {
		if strings.HasSuffix(fileInfo.Name(), ".html") {
			if !contains(templates, fileInfo.Name()) {
				//log.Println(fileInfo.Name())
				this.template.ParseFiles(path + fileInfo.Name())
			}
		}
	}

	return nil
}
Example #2
0
func (this *Taxidermist) Present(w io.Writer) error {
	var err error

	// As far as I can tell the name is arbitrary.
	this.template = template.New("")
	this.template.Delims(DelimLeft, DelimRight)

	for _, tdir := range this.templatePaths {
		if err := this.loadPath(tdir); err != nil {
			return errors.Stack(err)
		}
	}

	// Now we can execute the template.
	// (output writer, template name, data)
	if err = this.template.ExecuteTemplate(w, "main.html", this); err != nil {
		return errors.Stack(err)
	}

	return nil
}