Example #1
0
// loads the templates, using constraints specified in the cfg provided at initialization.
// The templates are read from the embedded assets, no extesion observation is made. The
// global Funcs is used as the template funcmap.
func (t *Template) load() (*Template, error) {
	l := t.cfg.Name
	base := template.New(l).Funcs(Funcs)
	t.tpl = base
	var lerr error
	for _, dir := range t.cfg.IncludesDirs {
		for _, name := range asset.AssetNames() {
			if strings.HasPrefix(name, dir) {
				tpl := t.tpl.New(name)
				nd, err := asset.Asset(name)
				if err != nil {
					lerr = err
					break
				}
				_, err = tpl.Parse(string(nd))
				if err != nil {
					lerr = err
					break
				}
			}
		}
	}
	if lerr != nil {
		return nil, lerr
	}
	return t, nil
}
Example #2
0
// adds the content of the given file into the template document. This is a helper for
// adding scripts to the templates. The name should be a filepath to the template(that
// whose content we want to insert ).
//
// Example calling this func with name set to /desk/auth/date_picker.html will result
// in the file desk/auth/date_picker.html being injected into the caller template, the name
// is relative to the template root directory which in our case is templates.
func script(name string) template.HTML {
	n := strings.TrimPrefix(name, "/")
	b, err := asset.Asset(n)
	if err != nil {
		return template.HTML(err.Error())
	}
	return template.HTML(b)
}