当前位置: 首页>>代码示例>>Golang>>正文


Golang Template.Funcs方法代码示例

本文整理汇总了Golang中html/template.Template.Funcs方法的典型用法代码示例。如果您正苦于以下问题:Golang Template.Funcs方法的具体用法?Golang Template.Funcs怎么用?Golang Template.Funcs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在html/template.Template的用法示例。


在下文中一共展示了Template.Funcs方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: parseFiles

func parseFiles(filenames ...string) (*template.Template, error) {
	var t *template.Template = nil
	var err error = nil

	if len(filenames) == 0 {
		return nil, fmt.Errorf("html/template: no files named in call to ParseFiles")
	}

	sort.Strings(filenames)

	for _, filename := range filenames {
		if t == nil {
			t = template.New(filename)
		}
		if filename != t.Name() {
			t = t.New(filename)
		}
		_, err = t.Funcs(funcMap).Parse(templates[filename])

		// anyone template syntax error throw panic
		if err != nil {
			panic(err)
		}
	}
	return t, err
}
开发者ID:elago,项目名称:ela,代码行数:26,代码来源:template.go

示例2: ParseFile

func (t *Template) ParseFile(file string, override bool) error {
	dir := ""
	var err error
	if !override {
		dir, err = os.Getwd()
		if err != nil {
			return err
		}
	}
	var tmpl *template.Template
	if t.HtmlTemplate == nil {

		tmpl, err = template.ParseFiles(dir + "/" + file)
		if err != nil {
			return err
		}
		tmpl.Funcs(t.FuncMap)
	} else {
		t.HtmlTemplate.Funcs(t.FuncMap)
		tmpl, err = t.HtmlTemplate.ParseFiles(dir + "/" + file)
		if err != nil {
			log.Println(err)
			return err
		}
	}
	t.HtmlTemplate = tmpl

	return err
}
开发者ID:janiukjf,项目名称:GoAdmin,代码行数:29,代码来源:template.go

示例3: buildTemplate

func buildTemplate(dir string, funcMap template.FuncMap) (*template.Template, error) {
	var t *template.Template
	return t, filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			filetext, err := ioutil.ReadFile(path)
			if err != nil {
				return err
			}
			text := string(filetext)
			name := path[len(dir)+1:]
			name = strings.Replace(name, `\`, `/`, -1)

			var tmpl *template.Template
			if t == nil {
				t = template.New(name)
			}
			if name == t.Name() {
				tmpl = t
			} else {
				tmpl = t.New(name)
			}
			_, err = tmpl.Funcs(funcMap).Parse(text)
			if err != nil {
				return err
			}
		}
		return nil
	})
}
开发者ID:no2key,项目名称:her,代码行数:29,代码来源:template.go

示例4: parseTemplates

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

}
开发者ID:NOX73,项目名称:go-tanks,代码行数:27,代码来源:server.go

示例5: getTemplateInstance

func (v *View) getTemplateInstance(tpl []string) (*template.Template, error) {
	key := strings.Join(tpl, "-")
	// if IsCache, get cached template if exist
	if v.IsCache {
		if v.templateCache[key] != nil {
			return v.templateCache[key], nil
		}
	}
	var (
		t    *template.Template
		e    error
		file []string = make([]string, len(tpl))
	)
	for i, tp := range tpl {
		file[i] = path.Join(v.Dir, tp)
	}
	t = template.New(path.Base(tpl[0]))
	t.Funcs(v.FuncMap)
	t, e = t.ParseFiles(file...)
	if e != nil {
		return nil, e
	}
	if v.IsCache {
		v.templateCache[key] = t
	}
	return t, nil

}
开发者ID:Lao-liu,项目名称:GoInk,代码行数:28,代码来源:view.go

示例6: ParseFile

func (t *Template) ParseFile(file string, override bool) error {
	dir := ""
	var err error
	if !override {
		dir, err = os.Getwd()
		if err != nil {
			return err
		}
	}
	var tmpl *template.Template
	if t.HtmlTemplate == nil {

		tmpl, err = template.New(file).Funcs(t.FuncMap).ParseFiles(dir + "/" + file)
		if err != nil {
			return err
		}
		tmpl.Funcs(t.FuncMap)
	} else {
		// Make sure the FuncMap is added to the template before parsing the new file
		t.HtmlTemplate.Funcs(t.FuncMap)
		tmpl, err = t.HtmlTemplate.ParseFiles(dir + "/" + file)
		if err != nil {
			return err
		}
	}
	t.HtmlTemplate = tmpl

	return err
}
开发者ID:ninnemana,项目名称:goengine,代码行数:29,代码来源:template.go

示例7: setFuncs

func setFuncs(t *template.Template) *template.Template {
	t.Funcs(template.FuncMap{
		"inc":      inc,
		"split":    split,
		"gravatar": gravatar,
		"timeAgo":  timeAgo,
	})
	return t
}
开发者ID:composehub,项目名称:server,代码行数:9,代码来源:main.go

示例8: addYield

func (r *templateRender) addYield(t *template.Template, name string, binding interface{}) {
	funcs := template.FuncMap{
		"yield": func() (template.HTML, error) {
			buf, err := r.execute(t, name, binding)
			// return safe html here since we are rendering our own template
			return template.HTML(buf.String()), err
		},
	}
	t.Funcs(funcs)
}
开发者ID:mbict,项目名称:render,代码行数:10,代码来源:template.go

示例9: applyFilters

// applyFilters sets functions of template.
func applyFilters(t *template.Template, filters ...Filter) *template.Template {
	funcMap := template.FuncMap{}
	for _, filter := range filters {
		if filter.Func == nil {
			continue
		}
		funcMap[filter.Name] = filter.Func
	}
	return t.Funcs(funcMap)
}
开发者ID:gophergala2016,项目名称:source,代码行数:11,代码来源:template.go

示例10: AddHtmlRegisterFunc

// AddRegisterFunc provides an easy to add function for getting a Register to your html/template.Template
func AddHtmlRegisterFunc(tmpl *htmltmpl.Template, name string) *htmltmpl.Template {
	if name == "" {
		name = "getregister"
	}
	tmpl.Funcs(map[string]interface{}{
		name: func() *Register {
			return NewRegister()
		},
	})
	return tmpl
}
开发者ID:valm0unt,项目名称:goutils,代码行数:12,代码来源:main.go

示例11: addYield

func (r *TplRender) addYield(t *template.Template, tplName string, data interface{}) {
	funcs := template.FuncMap{
		"yield": func() (template.HTML, error) {
			buf, err := r.execute(t, tplName, data)
			// return safe html here since we are rendering our own template
			return template.HTML(buf.String()), err
		},
		"current": func() (string, error) {
			return tplName, nil
		},
	}
	t.Funcs(funcs)
}
开发者ID:Xetius,项目名称:grafana,代码行数:13,代码来源:render.go

示例12: CompileWithTemplate

// Same as Compile but allows to specify a template
func (c *Compiler) CompileWithTemplate(t *template.Template) (*template.Template, error) {
	data, err := c.CompileString()

	if err != nil {
		return nil, err
	}

	tpl, err := t.Funcs(FuncMap).Parse(data)

	if err != nil {
		return nil, err
	}

	return tpl, nil
}
开发者ID:reqshark,项目名称:amber,代码行数:16,代码来源:compiler.go

示例13: RenderTemplate

func RenderTemplate(w http.ResponseWriter, layout, name string, data interface{}) {
	templates := strings.Split(name, "/")
	if len(templates) != 2 {
		http.Error(w, "Wrong Render Path: "+name, http.StatusInternalServerError)
		return
	}
	var ts *template.Template
	var err error
	mainPath, err := filepath.Abs(viewConf.Folder + string(os.PathSeparator) + viewConf.LayoutFolder + string(os.PathSeparator) + layout + "." + viewConf.PostFix)
	if err != nil {
		http.Error(w, "Main File Abs Error: "+err.Error(), http.StatusInternalServerError)
		return
	}
	renderPath, err := filepath.Abs(viewConf.Folder + string(os.PathSeparator) + name + "." + viewConf.PostFix)
	if err != nil {
		http.Error(w, "Render File Abs Error: "+err.Error(), http.StatusInternalServerError)
		return
	}
	if viewConf.IsCache {
		tmpKey := layout + "_" + name
		var ok bool
		if ts, ok = cacheTmp[tmpKey]; ok {
			ts = cacheTmp[tmpKey]
		} else {
			if ts, err = template.ParseFiles(mainPath, renderPath); err != nil {
				http.Error(w, "Template Parse Error: "+err.Error(), http.StatusInternalServerError)
				return
			}
			cacheTmp[tmpKey] = ts
		}
	} else {
		if ts, err = template.ParseFiles(mainPath, renderPath); err != nil {
			http.Error(w, "Template Parse Error: "+err.Error(), http.StatusInternalServerError)
			return
		}
	}

	err = ts.Funcs(funcMaps).ExecuteTemplate(w, layout, data)
	if err != nil {
		http.Error(w, "Template Execute Error: "+err.Error(), http.StatusInternalServerError)
		return
	}
}
开发者ID:DavidZhangqin,项目名称:goweb,代码行数:43,代码来源:render.go

示例14: addPartial

// Add partial keyword
func addPartial(t *template.Template) {
	funcs := template.FuncMap{
		"partial": func(name string, pairs ...interface{}) (template.HTML, error) {
			binding, err := mapFromPairs(pairs...)

			if err != nil {
				return "", err
			}

			dir, filename := filepath.Split(name)

			buf, err := executeTemplate(t, dir+"_"+filename+".html", binding)

			// return safe html
			return template.HTML(buf.String()), err
		},
	}
	t.Funcs(funcs)
}
开发者ID:8protons,项目名称:wutrender,代码行数:20,代码来源:wutrender.go

示例15: getInstance

func (this *View) getInstance(tpl string) (*template.Template, error) {
	if this.IsCache && this.templateCache[tpl] != nil {
		return this.templateCache[tpl], nil
	}
	var (
		t *template.Template
		e error
	)
	t = template.New(path.Base(tpl))
	t.Funcs(this.FuncMap)
	t, e = t.ParseFiles(path.Join(this.Dir, tpl))
	if e != nil {
		return nil, e
	}
	if this.IsCache {
		this.templateCache[tpl] = t
	}
	return t, nil
}
开发者ID:vckai,项目名称:GoAnswer,代码行数:19,代码来源:view.go


注:本文中的html/template.Template.Funcs方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。