예제 #1
0
func main() {
	var err error
	var tpl *template.Template
	tpl, err = tpl.ParseGlob("templates/*.gohtml")
	if err != nil {
		log.Fatalln(err)
	}

	err = tpl.Execute(os.Stdout, Page{
		Title: "My Title 2",
		Body:  "hello world",
	})
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("\n***************")

	err = tpl.ExecuteTemplate(os.Stdout, "tpl.gohtml", Page{
		Title: "My Title 2",
		Body:  "hello world",
	})
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("\n***************")

	err = tpl.ExecuteTemplate(os.Stdout, "tpl2.gohtml", Page{
		Title: "My Title 2",
		Body:  "hello world",
	})
	if err != nil {
		log.Fatalln(err)
	}
}
예제 #2
0
파일: server.go 프로젝트: NOX73/go-tanks
func (s *Server) parseTemplates() {

	var t *template.Template

	t = template.New("layout")
	t.Funcs(template.FuncMap{"ng": func(s string) string { return "{{" + s + "}}" }})

	_, err := t.ParseGlob(viewPath("**.html"))
	if err != nil {
		log.Fatal(err)
	}

	subdirs, _ := ioutil.ReadDir(viewPath(""))
	for _, dir := range subdirs {
		if !dir.IsDir() {
			continue
		}
		fullPath := viewPath(dir.Name())
		_, err := t.ParseGlob(path.Join(fullPath, "*.html"))
		if err != nil {
			log.Fatal(err)
		}
	}

	s.templates["layout"] = t

}
예제 #3
0
func readtemplates(dir string, titles map[Language]string) (err error) {
	var templates *template.Template
	templates, err = template.New("base").Funcs(tmplFuncs).ParseGlob(dir + "/*.tmpl")
	if err != nil {
		return
	}
	var d *os.File
	d, err = os.Open(dir)
	if err != nil {
		return
	}
	var v []os.FileInfo
	v, err = d.Readdir(0)
	if err != nil {
		return
	}
	for _, fi := range v {
		if fi.IsDir() {
			var t *template.Template
			t, err = templates.Clone()
			if err != nil {
				return
			}
			subdir := dir + "/" + fi.Name()
			_, err = t.ParseGlob(subdir + "/*.tmpl")
			if err != nil {
				return
			}
			th := t.Lookup("home")
			ti := t.Lookup("info")
			if th == nil {
				return fmt.Errorf(`Template "home" is missing in %s`, subdir)
			}
			if ti == nil {
				return fmt.Errorf(`Template "info" is missing in %s`, subdir)
			}
			title, ok := "", false
			if title, ok = titles[Language(fi.Name())]; !ok {
				if title, ok = titles[defaultlang]; !ok {
					title = "Uploader"
				}
			}
			langtmpl[Language(fi.Name())] = &tmpl{title, th, ti}
		}
	}
	defaulttmpl = langtmpl[defaultlang]
	if defaulttmpl == nil {
		fmt.Errorf("missing " + string(defaultlang) + " template")
	}
	languages = make([]Language, 0, len(langtmpl))
	languages = append(languages, defaultlang)
	for k := range langtmpl {
		if k != defaultlang {
			languages = append(languages, k)
		}
	}
	return
}
예제 #4
0
func (this *PageTemplate) init() (*template.Template, error) { // {{{
	var err error
	var tmpl *template.Template

	if tmpl, err = template.ParseFiles(path.Join(this.Path, TEMPLATE_DIR_MAIN, this.Name, TEMPLATE_LAYOUT)); err != nil {
		return nil, err
	}

	if tmpl, err = tmpl.ParseGlob(path.Join(this.Path, TEMPLATE_DIR_MAIN, this.Name, TEMPLATE_DIR_PARTIAL, TEMPLATE_ALL)); err != nil {
		return nil, err
	}

	return tmpl, nil
} // }}}
예제 #5
0
func (this *PageTemplate) Execute(w http.ResponseWriter, page *Page) error { // {{{
	var err error
	var tmpl *template.Template

	if tmpl, err = this.init(); err != nil {
		return err
	}

	if tmpl, err = tmpl.ParseGlob(path.Join(this.Path, TEMPLATE_DIR_MAIN, this.Name, TEMPLATE_DIR_VIEW, page.Name(), TEMPLATE_ALL)); err != nil {
		return err
	}

	if err = tmpl.Execute(w, page); err != nil {
		return err
	}

	return nil
} // }}}
예제 #6
0
func get(driver *Template, name string) *Template {

	var contents string
	var t *template.Template
	var err error

	pth := "resources/template/" + name + ".tmpl"
	partials := filepath.Join(filepath.Dir(pth), "partials", "_*.tmpl")

	contents, err = load(pth)
	if err != nil {
		return &Template{nil, err}
	}

	if driver != nil {
		t, err = driver.Clone()
		if err != nil {
			return &Template{nil, err}
		}

		_, err = t.Parse(contents)
		for _, name := range blocknames {
			if found := t.Lookup(name); found == nil {
				t.Parse("{{ define `" + name + "`}}{{ end }}")
			}
		}

		t.ParseGlob(partials)

	} else {
		t = template.New(name)
		if err != nil {
			return &Template{nil, err}
		}
		t, err = t.Funcs(funcMap).Parse(contents)
	}
	return &Template{t, err}
}
예제 #7
0
파일: template.go 프로젝트: orian/template
func (o *opParseGlob) Run(t *htmlTmpl.Template) (*htmlTmpl.Template, error) {
	if t == nil {
		return htmlTmpl.ParseGlob(o.pattern)
	}
	return t.ParseGlob(o.pattern)
}
예제 #8
0
func (p *Package) addLib(tpl *template.Template) {
	glob := p.path + string(os.PathSeparator) + p.libPath + "/*.html"
	log.Printf("Adding lib: %v", glob)
	tpl.ParseGlob(glob)
}