本文整理汇总了Golang中html/template.Template.New方法的典型用法代码示例。如果您正苦于以下问题:Golang Template.New方法的具体用法?Golang Template.New怎么用?Golang Template.New使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类html/template.Template
的用法示例。
在下文中一共展示了Template.New方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: initTemplates
func (r *TemplateRender) initTemplates(t *template.Template, tmps []string, exclude *[]string) error {
firstLevel := 0 == len(*exclude)
for tkey, tpl := range r.prepareTemplates(tmps...) {
if nil == t.Lookup(tkey) {
if data, err := ioutil.ReadFile(tpl); nil == err {
tmps := templatesRegex.FindAllStringSubmatch(string(data), -1)
ntemplates := []string{}
if nil != tmps && len(tmps) > 0 {
for _, it := range tmps {
if sIndexOf(it[1], *exclude) < 0 {
*exclude = append(*exclude, it[1])
ntemplates = append(ntemplates, it[1])
}
}
}
// Prepare new templates
if len(ntemplates) > 0 {
if err = r.initTemplates(t, ntemplates, exclude); nil != err {
return err
}
}
if _, err = t.New(tkey).Parse(string(data)); nil != err {
return err
}
} else if firstLevel {
return err
}
}
}
return nil
}
示例3: 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
}
示例4: LoadPartialTemplates
func LoadPartialTemplates(appDir string, partialTemplatePaths []string, collectorTemplate *template.Template) *template.Template {
if len(partialTemplatePaths) < 1 {
log.Printf("Expect partial templates to be len > 0")
return collectorTemplate
}
for _, path := range partialTemplatePaths {
input, err := ioutil.ReadFile(path)
if err != nil {
log.Print(err)
continue
}
//HACK revisit using template.Must
//collectorTemplate, err = collectorTemplate.Clone()
if err != nil {
log.Printf("Error was %s \n", err)
return collectorTemplate
}
name := ConvertTemplateName(appDir, path)
//fmt.Printf("templateName = %s \n", name)
collectorTemplate = template.Must(collectorTemplate.New(name).Parse(string(input)))
}
//TODO add this loop when debug flag is supported
// for _, tp := range collectorTemplate.Templates() {
// fmt.Printf("tp.Name = %s \n", tp.Name())
// }
return collectorTemplate
}
示例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: 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
}
示例7: 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
}
示例8: 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
}
示例9: 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
}
示例10: 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
})
}
示例11: loadTemplate
func (v *Views) loadTemplate(name, content string, t *template.Template) {
log.Println("loaded template", name)
_, err := t.New(name).Parse(content)
if err != nil {
log.Panic(err)
}
}
示例12: loadBaseTemplates
// loadTemplates walks sourceDir and loads all base templates into t. These
// are files that have a '.tmpl' extensions and a leading underscore. The
// template name is the filename with those stripped.
func loadBaseTemplates(t *template.Template) error {
return filepath.Walk(*sourceDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
basename := filepath.Base(path)
// Only handle files that start with _
if !strings.HasPrefix(basename, "_") {
return nil
}
// Only handle files with ".tmpl" extension
ext := filepath.Ext(basename)
if ext != ".tmpl" {
return nil
}
fmt.Printf("Loading template file: %v\n", path)
// Strip off "_" and ".tmpl"
name := strings.TrimPrefix(strings.TrimSuffix(basename, filepath.Ext(basename)), "_")
data, err := ioutil.ReadFile(path)
_, err = t.New(name).Parse(string(data))
if err != nil {
return err
}
return nil
})
}
示例13: getMethodTemplate
func (f *RestHtmlFormattor) getMethodTemplate(m *template.Template, rest *RestContext) *template.Template {
t := m.Lookup(rest.Method + f.dotsuffix)
var err error
if t == nil {
t, err = m.New(rest.Method + f.dotsuffix).ParseFiles(filepath.Join(document_root, rest.Url, rest.Method+f.dotsuffix))
if err != nil {
fmt.Println("ERROR template.ParseFile: %v", err)
}
}
return t
}
示例14: parseTemplate
func parseTemplate(name string, content string) {
if tmpl == nil {
tmpl = template.New(name)
}
var t *template.Template
if tmpl.Name() == name {
t = tmpl
} else {
t = tmpl.New(name)
}
template.Must(t.New(name).Delims("[[", "]]").Parse(content))
}
示例15: LoadVirtualTemplateFile
// LoadVirtualTemplateFile loads up a virtualfile into a template
func LoadVirtualTemplateFile(vf *VFile, tree *template.Template) (*template.Template, error) {
contents, ex := vf.Data()
if ex != nil {
return nil, ex
}
tl := tree.New(vf.Name())
tl, ex = tl.Parse(string(contents))
return tl, ex
}