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


Golang Template.Parse方法代码示例

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


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

示例1: common

func common(t *template.Template) (*template.Template, error) {
	return t.Parse(`
{{ define "header" }}
// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.

// Package {{ .PackageName }} provides a client for {{ .FullName }}.
package {{ .PackageName }}

import (
  "net/http"
  "time"

  "github.com/awslabs/aws-sdk-go/aws"
  "github.com/awslabs/aws-sdk-go/gen/endpoints"
)

{{ end }}

{{ define "footer" }}
// avoid errors if the packages aren't referenced
var _ time.Time
{{ end }}

`)
}
开发者ID:2722,项目名称:lantern,代码行数:25,代码来源:templates.go

示例2: loadTemplateInto

func loadTemplateInto(fs vfs.Opener, tpl *template.Template, fName string) (*template.Template, error) {
	src, err := templateSource(fs, fName)
	if err != nil {
		return nil, err
	}
	return tpl.Parse(src)
}
开发者ID:opentable,项目名称:sous,代码行数:7,代码来源:renderer.go

示例3: formatErrorDescription

// formatErrorDescription takes a string in the default text/template
// format and converts it to a string with replacements. The fields come
// from the ErrorDetails struct and vary for each type of error.
func formatErrorDescription(s string, details ErrorDetails) string {

	var tpl *template.Template
	var descrAsBuffer bytes.Buffer
	var err error

	if errorTemplates == nil {
		errorTemplates = template.New("all-errors")
	}

	tpl = errorTemplates.Lookup(s)
	if tpl == nil {
		tpl = errorTemplates.New(s)
		tpl, err = tpl.Parse(s)
		if err != nil {
			return err.Error()
		}
	}

	err = tpl.Execute(&descrAsBuffer, details)
	if err != nil {
		return err.Error()
	}

	return descrAsBuffer.String()
}
开发者ID:aws,项目名称:amazon-ecs-cli,代码行数:29,代码来源:errors.go

示例4: parseFromFile

func parseFromFile(t *template.Template, fname string) (*template.Template, error) {
	buf, err := ioutil.ReadFile(fname)
	if err != nil {
		return nil, err
	}
	return t.Parse(string(buf))
}
开发者ID:andrius4669,项目名称:afcgw,代码行数:7,代码来源:tmpl.go

示例5: Render

func Render(templateName string, options interface{}) (string, error) {
	asset, err := Asset(templateName)
	if err != nil {
		return "", maskAny(err)
	}

	// parse template
	var tmpl *template.Template
	tmpl = template.New(templateName)
	funcMap := template.FuncMap{
		"escape":     escape,
		"quote":      strconv.Quote,
		"yamlPrefix": yamlPrefix,
	}
	tmpl.Funcs(funcMap)
	_, err = tmpl.Parse(string(asset))
	if err != nil {
		return "", maskAny(err)
	}
	// write file to buffer
	tmpl.Funcs(funcMap)
	buffer := &bytes.Buffer{}
	err = tmpl.Execute(buffer, options)
	if err != nil {
		return "", maskAny(err)
	}

	return buffer.String(), nil
}
开发者ID:pulcy,项目名称:quark,代码行数:29,代码来源:render.go

示例6: Transform

func (ctx *TemplateContext) Transform(writer io.Writer, descriptor string) error {
	var t *template.Template

	if b, err := ioutil.ReadFile(descriptor); err != nil {
		return err
	} else {
		var e error
		t = template.New(descriptor).Funcs(Funcs)
		t, e = t.Parse(string(b))
		if e != nil {
			return e
		}
		if matches, err := filepath.Glob("./**/*.tmpl"); err == nil && len(matches) > 0 {
			if t, e = t.ParseFiles(matches...); err != nil {
				return err
			}
		}
	}
	environment := viper.GetString(ENV_NAME)
	m := ctx.mergeAppWithDefault(strings.ToLower(environment))

	if err := t.Execute(writer, m); err != nil {
		return err
	}
	return nil
}
开发者ID:ContainX,项目名称:depcon,代码行数:26,代码来源:templatectx.go

示例7: Render

// Render updates the given destinationPath according to the given template and options.
// Returns true if the file was created or changed, false if nothing has changed.
func Render(templateName, destinationPath string, options interface{}, destinationFileMode os.FileMode) (bool, error) {
	asset, err := Asset(templateName)
	if err != nil {
		return false, maskAny(err)
	}

	// parse template
	var tmpl *template.Template
	tmpl = template.New(templateName)
	funcMap := template.FuncMap{
		"escape": escape,
		"quote":  strconv.Quote,
	}
	tmpl.Funcs(funcMap)
	_, err = tmpl.Parse(string(asset))
	if err != nil {
		return false, maskAny(err)
	}
	// execute template to buffer
	buf := &bytes.Buffer{}
	tmpl.Funcs(funcMap)
	err = tmpl.Execute(buf, options)
	if err != nil {
		return false, maskAny(err)
	}

	// Update file
	changed, err := util.UpdateFile(destinationPath, buf.Bytes(), destinationFileMode)
	return changed, maskAny(err)
}
开发者ID:pulcy,项目名称:gluon,代码行数:32,代码来源:render.go

示例8: reload

func reload(ctx dingo.Context, t *template.Template, v []View) error {
	for _, view := range v {
		if err := reload(ctx, t, view.Extensions()); err != nil {
			return err
		} else if t, err = t.Parse(string(view.Data(ctx))); err != nil {
			log.Printf("Failed to parse extension: (%s)\n%s\n", view.Name(), err)
		}
	}
	return nil
}
开发者ID:juztin,项目名称:dingo,代码行数:10,代码来源:view.go

示例9: 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

示例10: evalMigration

func (m *Migrator) evalMigration(tmpl *template.Template, sql string) (string, error) {
	tmpl, err := tmpl.Parse(sql)
	if err != nil {
		return "", err
	}

	var buf bytes.Buffer
	err = tmpl.Execute(&buf, m.Data)
	if err != nil {
		return "", err
	}

	return buf.String(), nil
}
开发者ID:jackc,项目名称:tern,代码行数:14,代码来源:migrate.go

示例11: 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

示例12: ParseTemplate

/*
** Render a template string with given data
 */
func ParseTemplate(tmpl string, data metadata.Metamap) (string, error) {
	var buf bytes.Buffer
	var tpl *template.Template

	tpl = createTemplate()
	tpl, err := tpl.Parse(tmpl)
	if err != nil {
		return "", err
	}

	err = tpl.Execute(&buf, data)
	if err != nil {
		return "", err
	}
	return buf.String(), nil
}
开发者ID:calj,项目名称:kaigara,代码行数:19,代码来源:render.go

示例13: 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

示例14: packTemplate

func packTemplate(tplPack *template.Template, name string) (tpl *template.Template) {

	cwd, err := os.Getwd()
	M.PANIC_ON_ERR(err)

	f, err := os.Open(fmt.Sprintf("%s/%s.html", path.Join(cwd, "templates"), name))
	M.PANIC_ON_ERR(err)
	defer f.Close()

	html, err := ioutil.ReadAll(f)
	M.PANIC_ON_ERR(err)

	tpl, err = tplPack.Parse(string(html))
	M.PANIC_ON_ERR(err)

	return
}
开发者ID:hoffoo,项目名称:stayke.net,代码行数:17,代码来源:site.go

示例15: parseData

func parseData(temp *template.Template, reader io.ReadCloser) (*template.Template, error) {
	data, err := ioutil.ReadAll(reader)
	if err != nil {
		return temp, err
	}
	reader.Close()

	if len(data) > 0 {
		if temp == nil {
			x := template.New("new")
			x.Parse(string(data))
			return x, nil
		} else {
			temp.Parse(string(data))
			return temp, nil
		}
	}
	return temp, fmt.Errorf("no data")
}
开发者ID:FactomProject,项目名称:factomd,代码行数:19,代码来源:parse.go


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