本文整理汇总了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
}
示例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
}
示例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
})
}
示例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
}
示例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
}
示例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
}
示例7: setFuncs
func setFuncs(t *template.Template) *template.Template {
t.Funcs(template.FuncMap{
"inc": inc,
"split": split,
"gravatar": gravatar,
"timeAgo": timeAgo,
})
return t
}
示例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)
}
示例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)
}
示例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
}
示例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)
}
示例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
}
示例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
}
}
示例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)
}
示例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
}