本文整理汇总了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 }}
`)
}
示例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)
}
示例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()
}
示例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))
}
示例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
}
示例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
}
示例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)
}
示例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
}
示例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)
}
示例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
}
示例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))
}
示例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
}
示例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
}
示例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
}
示例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")
}