本文整理汇总了Golang中html/template.Template.Name方法的典型用法代码示例。如果您正苦于以下问题:Golang Template.Name方法的具体用法?Golang Template.Name怎么用?Golang Template.Name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类html/template.Template
的用法示例。
在下文中一共展示了Template.Name方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: parseTemplates
func parseTemplates(tpl *template.Template) (err error) {
templatesToBeParsed := []string{tpl.Name()}
for len(templatesToBeParsed) != 0 {
name := templatesToBeParsed[0]
templatesToBeParsed = templatesToBeParsed[1:]
parsed := false
for _, t := range tpl.Templates() {
if tpl.Name() != name && t.Name() == name {
parsed = true
break
}
}
if !parsed {
content, err := readTemplate(name)
if err != nil {
return err
}
_, err = tpl.Parse(content)
check(err)
for _, matched := range templateKeyword.FindAllStringSubmatch(content, -1) {
templatesToBeParsed = append(templatesToBeParsed, matched[1])
}
}
}
return
}
示例2: parseFiles
func parseFiles(filenames ...string) (*template.Template, error) {
if len(filenames) == 0 {
// Not really a problem, but be consistent.
return nil, fmt.Errorf("template: no files named in call to ParseFiles")
}
var t *template.Template
for _, filename := range filenames {
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
s := string(b)
name := filepath.Base(filename)
var tmpl *template.Template
if t == nil {
t = template.New(name).Funcs(baseFuncs)
}
if name == t.Name() {
tmpl = t
} else {
tmpl = t.New(name)
}
_, err = tmpl.Parse(s)
if err != nil {
return nil, err
}
}
return t, nil
}
示例3: LoadTemplates
func LoadTemplates() (*template.Template, error) {
// Below is taken from http://golang.org/src/html/template/template.go
// because there is no way to get all the templates from the in-memory filesystem.
// We would like to use e.g. ParseFiles but that is hard-coded
// to use an actual filesystem; we cannot retarget it.
var t *template.Template
for _, filename := range filenames {
s, err := templates.FSString(false, filename)
if err != nil {
return nil, err
}
name := filepath.Base(filename)
// First template becomes return value if not already defined,
// and we use that one for subsequent New calls to associate
// all the templates together. Also, if this file has the same name
// as t, this file becomes the contents of t, so
// t, err := New(name).Funcs(xxx).ParseFiles(name)
// works. Otherwise we create a new template associated with t.
var tmpl *template.Template
if t == nil {
t = template.New(name)
}
if name == t.Name() {
tmpl = t
} else {
tmpl = t.New(name)
}
_, err = tmpl.Parse(s)
if err != nil {
return nil, err
}
}
return t, nil
}
示例4: parseTemplates
func parseTemplates(filenames ...string) (*template.Template, error) {
var t *template.Template
for _, filename := range filenames {
b, err := loadTemplate(filename)
if err != nil {
return nil, err
}
s := string(b)
name := filepath.Base(filename)
// First template becomes return value if not already defined,
// and we use that one for subsequent New calls to associate
// all the templates together. Also, if this file has the same name
// as t, this file becomes the contents of t, so
// t, err := New(name).Funcs(xxx).ParseFiles(name)
// works. Otherwise we create a new template associated with t.
var tmpl *template.Template
if t == nil {
t = template.New(name)
}
if name == t.Name() {
tmpl = t
} else {
tmpl = t.New(name)
}
_, err = tmpl.Parse(s)
if err != nil {
return nil, err
}
}
return t, nil
}
示例5: LoadDirectory
func (t *BinTemplate) LoadDirectory(directory string) (*template.Template, error) {
var tmpl *template.Template
files, err := t.AssetDir(directory)
if err != nil {
return tmpl, err
}
for _, filePath := range files {
contents, err := t.Asset(directory + "/" + filePath)
if err != nil {
return tmpl, err
}
name := filepath.Base(filePath)
if tmpl == nil {
tmpl = template.New(name)
}
if name != tmpl.Name() {
tmpl = tmpl.New(name)
}
if _, err = tmpl.Parse(string(contents)); err != nil {
return tmpl, err
}
}
return tmpl, nil
}
示例6: 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
})
}
示例7: 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
}
示例8: applyBody
// applyBody
func applyBody(t *template.Template, names, bodies []string) (*template.Template, error) {
for i := 0; i < len(names); i++ {
name, body := names[i], bodies[i]
var tmpl *template.Template
if t == nil {
t = template.New(name)
}
if name == t.Name() {
tmpl = t
} else {
tmpl = t.New(name)
}
if len(Template.Filters) > 0 {
tmpl = applyFilters(tmpl, Template.Filters...)
}
if Template.Delims.isValid() {
tmpl.Delims(Template.Delims.Get())
}
DebugPrintf("Parse as \"%s\"\n", name)
_, err := tmpl.Parse(body)
if err != nil {
return nil, err
}
}
return t, nil
}
示例9: mkHttpHandler
func mkHttpHandler(h Handler, tpl *template.Template) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
sess, err := getSess(req)
if err != nil {
req.Cookies()
log.Printf("Error while getting session: %s", err)
rw.Write([]byte("Unable to create a session. Try reloading the page or delete the cookies for this site."))
}
user := userFromSess(sess)
outdata, user := h(user, sess, req)
mail := ""
if user != nil {
mail = user.Email()
}
if err := sess.Save(req, rw); err != nil {
log.Printf("Error while saving session: %s", err)
}
if err := tpl.Execute(rw, &tpldata{mail, outdata}); err != nil {
log.Printf("Error executing template %s: %s", tpl.Name(), err)
}
}
}
示例10: parseBindataFiles
// parseFiles is the helper for the method and function. If the argument
// template is nil, it is created from the first file.
func parseBindataFiles(filenames ...string) (*template.Template, error) {
var t *template.Template
if len(filenames) == 0 {
// Not really a problem, but be consistent.
return nil, fmt.Errorf("html/template: no files named in call to ParseFiles")
}
for _, filename := range filenames {
b, err := templates.Asset(filename)
if err != nil {
return nil, err
}
s := string(b)
name := filepath.Base(filename)
// First template becomes return value if not already defined,
// and we use that one for subsequent New calls to associate
// all the templates together. Also, if this file has the same name
// as t, this file becomes the contents of t, so
// t, err := New(name).Funcs(xxx).ParseFiles(name)
// works. Otherwise we create a new template associated with t.
var tmpl *template.Template
if t == nil {
t = template.New(name)
}
if name == t.Name() {
tmpl = t
} else {
tmpl = t.New(name)
}
_, err = tmpl.Parse(s)
if err != nil {
return nil, err
}
}
return t, nil
}
示例11: ShortcodeRender
func ShortcodeRender(tmpl *template.Template, data *ShortcodeWithPage) string {
buffer := new(bytes.Buffer)
err := tmpl.Execute(buffer, data)
if err != nil {
fmt.Println("error processing shortcode", tmpl.Name(), "\n ERR:", err)
}
return buffer.String()
}
示例12: tplHandler
func tplHandler(t *template.Template) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("Handling", r.URL, "with template", t.Name())
if err := t.Execute(w, nil); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
}
示例13: templateToString
// returns "" on error
func templateToString(t *template.Template, data interface{}) string {
var buf bytes.Buffer
err := t.Execute(&buf, data)
if err != nil {
logger.Errorf("Failed to execute template '%s', error: %s", t.Name(), err.Error())
return ""
}
return string(buf.Bytes())
}
示例14: renderTemplate
// Render a named template name to the HTTP channel
func renderTemplate(w io.Writer, tmpl template.Template, context interface{}) {
log.Printf("Rendering %s", tmpl.Name())
err := tmpl.ExecuteTemplate(w, "base.html", context)
if err != nil {
log.Printf("ERROR: %s", err)
w.Write([]byte("Unable to render"))
return
}
}
示例15: renderShortcodeWithPage
func renderShortcodeWithPage(tmpl *template.Template, data *ShortcodeWithPage) string {
buffer := bp.GetBuffer()
defer bp.PutBuffer(buffer)
err := tmpl.Execute(buffer, data)
if err != nil {
jww.ERROR.Println("error processing shortcode", tmpl.Name(), "\n ERR:", err)
jww.WARN.Println(data)
}
return buffer.String()
}