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


Golang Template.New方法代码示例

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


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

示例1: prepTemplate

func prepTemplate(parent *template.Template, fn, base string) error {
	t := parent.New(base)

	f, err := os.Open(fn)
	if err != nil {
		return err
	}
	defer f.Close()

	msg, err := mail.ReadMessage(f)
	if err != nil {
		return err
	}

	for k, v := range defaultHeaders {
		if msg.Header.Get(k) == "" {
			msg.Header[k] = v
		}
	}

	data := &bytes.Buffer{}

	// This is the only place I could find this method.  :/
	http.Header(msg.Header).Write(data)
	data.Write([]byte{'\r', '\n'})

	_, err = io.Copy(data, msg.Body)
	if err != nil {
		return err
	}

	_, err = t.Parse(data.String())
	return err
}
开发者ID:jk-cbfs,项目名称:cbugg,代码行数:34,代码来源:email.go

示例2: makeTemplates

func makeTemplates(t *[]TemplateConfig) (*template.Template, error) {
	var textTemplates *template.Template
	var err error
	templateFuncs := template.FuncMap{"padBefore": PadBefore}
	for i, templateConfig := range *t {
		t := ""
		h := ""
		for i, l := range templateConfig.Lines {
			if i > 0 {
				t = t + "\t"
				h = h + "\t"
			}
			t = t + l.Value
			h = h + l.Header
		}

		// Template proper
		if i == 0 {
			textTemplates, err = template.New(templateConfig.Name).Funcs(templateFuncs).Parse(t)
		} else {
			textTemplates, err = textTemplates.New(templateConfig.Name).Funcs(templateFuncs).Parse(t)
		}

		// Template header
		textTemplates, err = textTemplates.New(templateConfig.Name + "_H").Funcs(templateFuncs).Parse(h)

		if err != nil {
			break
		}
	}
	return textTemplates, err
}
开发者ID:japettyjohn,项目名称:layoutcsv,代码行数:32,代码来源:main.go

示例3: LoadTextOnto

// LoadText loads a template from a possibly inlined VFS as an associated template to the parent
func LoadTextOnto(fs vfs.Opener, parent *txttmpl.Template, tName, fName string) (*txttmpl.Template, error) {
	src, err := templateSource(fs, fName)
	if err != nil {
		return nil, err
	}
	var tpl *txttmpl.Template
	if parent == nil {
		tpl = txttmpl.New(tName)
	} else {
		tpl = parent.New(tName)
	}
	return tpl.Parse(src)
}
开发者ID:opentable,项目名称:sous,代码行数:14,代码来源:load.go

示例4: NewTemplating

func NewTemplating(partials *txttmpl.Template, filePath, content string) (*Templating, error) {
	t := Templating{
		name:      filePath,
		content:   CleanupOfTemplate(content),
		functions: TemplateFunctions,
	}
	if partials == nil {
		partials = txttmpl.New(t.name)
	}

	tmpl, err := partials.New(t.name).Funcs(t.functions).Funcs(map[string]interface{}(gtf.GtfFuncMap)).Parse(t.content)
	t.template = tmpl
	return &t, err
}
开发者ID:puckel,项目名称:dgr,代码行数:14,代码来源:templating.go

示例5: addTemplate

func addTemplate(t *template.Template, tname, fname string) (*template.Template, error) {
	buf, err := ioutil.ReadFile(fname)
	if err != nil {
		return nil, err
	}

	var nt *template.Template
	if t != nil {
		nt = t.New(tname)
	} else {
		nt = template.New(tname)
	}

	return nt.Parse(string(buf))
}
开发者ID:andrius4669,项目名称:afcgw,代码行数:15,代码来源:tmpl.go

示例6: Parse

// Parse parses declared templates.
func Parse(t *template.Template) (*template.Template, error) {
	for name, s := range templates {
		var tmpl *template.Template
		if t == nil {
			t = template.New(name)
		}
		if name == t.Name() {
			tmpl = t
		} else {
			tmpl = t.New(name)
		}
		if _, err := tmpl.Parse(s); err != nil {
			return nil, err
		}
	}
	return t, nil
}
开发者ID:ox0spy,项目名称:schematic,代码行数:18,代码来源:templates.go

示例7: SetTemplate

func SetTemplate(t *template.Template, name, filename string) error {

	// Read template
	buf, err := ioutil.ReadFile(filename)
	if err != nil {
		return err
	}

	// Update if exists
	if tt := t.Lookup(name); tt != nil {
		_, err = tt.Parse(string(buf))
		return err
	}

	// Allocate new name if not
	_, err = t.New(name).Parse(string(buf))
	return err
}
开发者ID:klauern,项目名称:caddy,代码行数:18,代码来源:template.go

示例8: LoadPartial

func (t *TemplateDir) LoadPartial() error {
	partials := []string{}

	directory, err := os.Open(t.src)
	if err != nil {
		return errs.WithEF(err, t.fields, "Failed to open template dir")
	}
	objects, err := directory.Readdir(-1)
	if err != nil {
		return errs.WithEF(err, t.fields, "Failed to read template dir")
	}
	for _, obj := range objects {
		if !obj.IsDir() && strings.HasSuffix(obj.Name(), ".partial") {
			partials = append(partials, t.src+"/"+obj.Name())
		}
	}

	if len(partials) == 0 {
		return nil
	}
	var tmpl *txttmpl.Template
	for _, partial := range partials {
		if tmpl == nil {
			tmpl = txttmpl.New(partial).Funcs(TemplateFunctions).Funcs(map[string]interface{}(gtf.GtfFuncMap))
		} else {
			tmpl = tmpl.New(partial).Funcs(TemplateFunctions).Funcs(map[string]interface{}(gtf.GtfFuncMap))
		}

		content, err := ioutil.ReadFile(partial)
		if err != nil {
			return errs.WithEF(err, t.fields.WithField("partial", partial), "Cannot read partial file")
		}
		tmpl, err = tmpl.Funcs(TemplateFunctions).Parse(CleanupOfTemplate(string(content)))
		if err != nil {
			return errs.WithEF(err, t.fields.WithField("partial", partial), "Failed to parse partial")
		}
	}
	t.Partials = tmpl
	return nil
}
开发者ID:puckel,项目名称:dgr,代码行数:40,代码来源:directory.go

示例9: loadTemplate

//loadTemplate 加载解析模板
func (d *dynamic) loadTemplate(currDir string) (*template.Template, error) {
	if len(d.Template) == 0 {
		return nil, fmt.Errorf(ErrorPageNotTemplate)
	}
	var (
		t                           *template.Template = nil
		filePath, fileName, content string
		b                           []byte
		err                         error
	)

	for _, filename := range d.Template {
		filePath = filepath.Join(currDir, filename)
		fileName = filepath.Base(filename)

		b, err = ioutil.ReadFile(filePath)
		if err != nil {
			return nil, err
		}
		content = d.formatTemplate(string(b))

		var tmpl *template.Template
		if t == nil {
			t = template.New(fileName)
		}
		if fileName == t.Name() {
			tmpl = t
		} else {
			tmpl = t.New(fileName)
		}
		tmpl.Delims(d.Delims.Left, d.Delims.Right)
		tmpl.Funcs(TemplateFuncMap)
		_, err = tmpl.Parse(content)
		if err != nil {
			return nil, err
		}
	}
	return t, nil
}
开发者ID:456vv,项目名称:birdswo,代码行数:40,代码来源:birdswo.go

示例10: NewSunnyPublisher

func NewSunnyPublisher(p string) *SunnyPublisher {
	var htmpl *htemplate.Template = htemplate.New("index.html")
	var tmpl *template.Template = template.New("index")
	var plen int = 0

	if strings.Contains(p, `\`) {
		p = strings.Replace(p, `\`, "/", -1)
	} else if p == "" {
		p = DEFAULT_VIEWPATH
	}

	if plen = len(p); p[plen-1] != "/" {
		p = p + "/"
		plen += 1
	}

	filepath.Walk(p, func(path string, info os.FileInfo, err error) error {
		if err == nil && !info.IsDir() {
			b, err := ioutil.ReadFile(path)

			if err == nil {
				if strings.HasSuffix(info.Name(), ".html") {
					htmpl.New(path[plen:]).Parse(string(b))
				} else {
					tmpl.New(path[plen:]).Parse(string(b))
				}
			}
		}

		return nil
	})

	return &SunnyPublisher{
		renderer: make(map[string]Renderer),
		htmpl:    htmpl,
		tmpl:     tmpl,
	}
}
开发者ID:zaolab,项目名称:sunnified,代码行数:38,代码来源:publisher.go

示例11: addTemplate

func addTemplate(base *template.Template, name string, filename string) *template.Template {
	text := loadTemplateText(filename)
	return template.Must(base.New(name).Parse(text))
}
开发者ID:vsouza,项目名称:storable,代码行数:4,代码来源:template.go


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