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


Golang Template.Clone方法代码示例

本文整理汇总了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,
	}
}
开发者ID:hooblei,项目名称:goven,代码行数:31,代码来源:site.go

示例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
}
开发者ID:alvaroloes,项目名称:sdkgen,代码行数:11,代码来源:generator.go

示例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
}
开发者ID:makyo,项目名称:juju,代码行数:40,代码来源:lxc-broker.go

示例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())
}
开发者ID:Adriano90,项目名称:weather-service,代码行数:6,代码来源:routes.go


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