本文整理汇总了Golang中text/template.Template.Clone方法的典型用法代码示例。如果您正苦于以下问题:Golang Template.Clone方法的具体用法?Golang Template.Clone怎么用?Golang Template.Clone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类text/template.Template
的用法示例。
在下文中一共展示了Template.Clone方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewTheme
func NewTheme(tpath string) *Theme {
var err error
var pattern string
var pages []string
var base *template.Template
var tmap = map[string]*template.Template{}
pattern = path.Join(tpath, "*.html")
base = template.Must(template.ParseGlob(pattern))
pattern = path.Join(tpath, "pages", "*.html")
if pages, err = filepath.Glob(pattern); err != nil {
panic(err)
}
for _, tpath := range pages {
var ts *template.Template
if ts, err = base.Clone(); err != nil {
panic(err)
}
if _, err = ts.ParseFiles(tpath); err != nil {
panic(err)
}
tmap[path.Base(tpath)] = ts
}
return &Theme{
Path: tpath,
Templates: tmap,
}
}
示例2: parseTemplates
func (g *Generator) parseTemplates(pathGlob string, baseTpl *template.Template) (fileNames []string, templates *template.Template, err error) {
fileNames, err = filepath.Glob(pathGlob)
if err != nil {
return nil, nil, errors.Annotate(err, "when reading files in "+pathGlob)
}
templates, err = template.Must(baseTpl.Clone()).ParseFiles(fileNames...)
if err != nil {
return nil, nil, errors.Annotate(err, "when parsing service templates files in "+pathGlob)
}
return
}
示例3: runTemplateCommand
// runTemplateCommand executes the given template with the given data,
// which generates a command to execute. If exitNonZeroOK is true, no
// error is returned if the exit code is not 0, otherwise an error is
// returned.
func runTemplateCommand(t *template.Template, exitNonZeroOK bool, data interface{}) (
exitCode int, err error,
) {
// Clone the template to ensure the original won't be changed.
cloned, err := t.Clone()
if err != nil {
return -1, errors.Annotatef(err, "cannot clone command template %q", t.Name())
}
var buf bytes.Buffer
if err := cloned.Execute(&buf, data); err != nil {
return -1, errors.Annotatef(err, "cannot execute command template %q", t.Name())
}
command := buf.String()
logger.Debugf("running command %q", command)
result, err := exec.RunCommands(exec.RunParams{Commands: command})
if err != nil {
return -1, errors.Annotatef(err, "cannot run command %q", command)
}
exitCode = result.Code
stdout := string(result.Stdout)
stderr := string(result.Stderr)
logger.Debugf(
"command %q returned code=%d, stdout=%q, stderr=%q",
command, exitCode, stdout, stderr,
)
if exitCode != 0 {
if exitNonZeroOK {
return exitCode, nil
}
return exitCode, errors.Errorf(
"command %q failed with exit code %d",
command, exitCode,
)
}
return 0, nil
}
示例4: Template
// Template uses the provided template definitions.
func (r *Router) Template(t *template.Template) {
r.Lock()
defer r.Unlock()
r.views = template.Must(t.Clone())
}