本文整理汇总了Golang中text/template.Template.ExecuteTemplate方法的典型用法代码示例。如果您正苦于以下问题:Golang Template.ExecuteTemplate方法的具体用法?Golang Template.ExecuteTemplate怎么用?Golang Template.ExecuteTemplate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类text/template.Template
的用法示例。
在下文中一共展示了Template.ExecuteTemplate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: renderTemplate
func renderTemplate(tmpl *template.Template, name string, data interface{}) string {
var buf bytes.Buffer
if err := tmpl.ExecuteTemplate(&buf, name, data); err != nil {
panic(err)
}
return buf.String()
}
示例2: ExecuteTemplate
// ExecuteTemplate executes a template on the source TeX files.
func (t *CompileTask) ExecuteTemplate(templ *template.Template, data interface{}, inputFilename string, outputFilename string) {
sc := t.context()
useTempFile := outputFilename == ""
if useTempFile {
outputFilename = sc.MustGetTempFile().Name()
}
inputFilename = sc.AbsPath(t.defaultCompileFilename(inputFilename))
f, err := os.Create(sc.AbsPath(outputFilename))
if err != nil {
panic(err)
}
w := io.Writer(f)
err = templ.ExecuteTemplate(w, filepath.Base(inputFilename), data)
if err != nil {
panic(err)
}
f.Close()
if useTempFile {
// copy back, remove temp
err = os.Remove(inputFilename)
if err != nil {
panic(err)
}
err = sc.CopyFile(outputFilename, inputFilename)
if err != nil {
panic(err)
}
}
}
示例3: templateWorker
// templateWorker create file from template
// done channel send nil or error back
func templateWorker(tpl *template.Template, ftpl FileTemplates, done chan error) {
// check file template path
ok, err := utils.PathExists(ftpl.Path)
if err != nil {
done <- err
return
}
// file template path not exists, create new file template path
if !ok {
if err := os.MkdirAll(ftpl.Path, 0755); err != nil {
done <- err
return
}
}
// create new target file
f, err := os.OpenFile(filepath.Join(ftpl.Path, ftpl.Target), os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
done <- err
return
}
defer f.Close()
// generate file content,
// send return to channel (nil or err)
done <- tpl.ExecuteTemplate(f, ftpl.Tpl, ftpl.Vars)
}
示例4: RenderPost
func (r Renderer) RenderPost(pc *PostContext, templates *template.Template) error {
err := os.MkdirAll(path.Join(r.OutputPath, pc.Slug), 0755)
if err != nil {
log.Panicf(err.Error())
}
outfile, err := os.Create(path.Join(r.OutputPath, pc.Slug, "index.html"))
if err != nil {
log.Panicf(err.Error())
}
err = templates.ExecuteTemplate(outfile, "post.html", pc)
if err != nil {
log.Panicf(err.Error())
}
// copy images
log.Printf("\"%s\": Using %d %s", pc.Slug, len(pc.Images), pluralize("image", len(pc.Images)))
for _, image := range pc.Images {
err = cp(image.SrcAbsPath, path.Join(r.OutputPath, pc.Slug, image.Filename))
if err != nil {
log.Panicf(err.Error())
}
}
log.Printf("\"%s\": Done rendering", pc.Slug)
return nil
}
示例5: processFile
func processFile(fc FileConfig, t *template.Template) {
// in file
iFile, err := os.Open(fc.Name)
if err != nil {
panic("Can't open " + fc.Name)
}
defer iFile.Close()
// out file
oFile, err := os.Create(fc.Name + "_out.txt")
if err != nil {
panic("Can't open/write to " + fc.Name + "_out.txt")
}
defer oFile.Close()
r := csv.NewReader(iFile)
r.Comma = '\t'
r.TrailingComma = true
record, err := r.Read()
for i := 0; err == nil; record, err = r.Read() {
if i == 0 {
t.ExecuteTemplate(oFile, fc.TemplateName+"_H", makeTemplateContext(&record))
}
i = i + 1
if i <= fc.Skip {
continue
}
oFile.Write([]byte("\n"))
t.ExecuteTemplate(oFile, fc.TemplateName, makeTemplateContext(&record))
}
fmt.Println(err)
oFile.Sync()
}
示例6: writeSingleJavaFile
func writeSingleJavaFile(tpl *template.Template, filedir string, javapackage string, templateName string, name string, data interface{}) {
jfile, err := os.Create(filepath.Join(filedir, strings.Title(name)+".java"))
defer jfile.Close()
dieIf(err)
fmt.Fprintf(jfile, "package %s;\n\n", javapackage)
err = tpl.ExecuteTemplate(jfile, templateName, data)
dieIf(err)
}
示例7: AddTemplate
func (sb *StringBuilder) AddTemplate(t *template.Template, name string, data interface{}) error {
buf := &bytes.Buffer{}
err := t.ExecuteTemplate(buf, name, data)
if err != nil {
return err
}
sb.Append(buf.String())
return nil
}
示例8: ExecuteTemplateAndWrite
// ExecuteTemplateAndWrite executes the template and writes the buffer to dst.
func (i *Invoice) ExecuteTemplateAndWrite(t *template.Template, name, dst string) error {
buf := &bytes.Buffer{}
if err := t.ExecuteTemplate(buf, name, i); err != nil {
return err
}
if err := ioutil.WriteFile(dst, buf.Bytes(), 0664); err != nil {
return err
}
return nil
}
示例9: implementAll
func implementAll(def ObjectDefinition, face *BaseInfo, code *bytes.Buffer, tmpl *template.Template) {
impl := face.GetObjectDefinition()
impl.ObjectName = def.ObjectName
err := tmpl.ExecuteTemplate(code, "object-implement", impl)
if err != nil {
fmt.Println(err.Error())
}
if hasParent(face) {
implementAll(def, face.GetParent(), code, tmpl)
}
}
示例10: renderTemplate
func renderTemplate(w http.ResponseWriter, tmpl string) {
var templates *template.Template
if config.liveTemplates {
templates = compileTemplates()
} else {
templates = precompiledTemplates
}
err := templates.ExecuteTemplate(w, tmpl+".html", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
示例11: NewTestHTMLObjectFieldsFn
func NewTestHTMLObjectFieldsFn(t *testing.T, tplate *template.Template, w io.Writer) func() string {
return func() string {
field := Field{Name: "FieldName", Class: Class{Name: "Class_Name", Project: Project{Name: "ProjectName"}}}
for _, tname := range fldtplates {
if err := tplate.ExecuteTemplate(w, tname, field); err != nil {
fmt.Println(err.Error())
t.Fail()
}
}
return ""
}
}
示例12: writeTableCrud
func writeTableCrud(w io.Writer, templates *template.Template, pkgName string, table Table) error {
return templates.ExecuteTemplate(w, "row", struct {
PkgName string
TableName string
StructName string
Columns []Column
PrimaryKeyColumns []*Column
}{
PkgName: pkgName,
TableName: table.TableName,
StructName: table.StructName,
Columns: table.Columns,
PrimaryKeyColumns: table.PrimaryKeyColumns,
})
}
示例13: Render
func Render(path string, w http.ResponseWriter, r *http.Request, ViewData map[string]interface{}) {
w.Header().Add("Content-Type", "text/html;charset=utf-8")
w.Header().Add("Server", "iron")
var tmpl *template.Template
if isTMPLAutoRefresh {
tmpl = template.Must(template.New(path).Delims("<?", "?>").ParseFiles(views[path].filenames...))
} else {
if nil == views[path].tmpl {
views[path].tmpl = template.Must(template.New(path).Delims("<?", "?>").ParseFiles(views[path].filenames...))
}
tmpl = views[path].tmpl
}
tmpl.ExecuteTemplate(w, "frame", ViewData)
}
示例14: ProcessEnum
func ProcessEnum(info *BaseInfo, code *bytes.Buffer, tmpl *template.Template) {
name := info.GetName()
prefix := GetCPrefix(info.GetNamespace())
def := &EnumDefinition{EnumName: name, CType: prefix + name}
numValues := info.GetNEnumValues()
for i := 0; i < numValues; i++ {
value := info.GetEnumValue(i)
valDef := EnumValue{Name: CamelCase(value.GetName()), EnumName: name, Value: value.GetValue()}
def.Values = append(def.Values, valDef)
}
err := tmpl.ExecuteTemplate(code, "enum", def)
if err != nil {
fmt.Println(err.Error())
}
}
示例15: alterFuncMap
// alterFuncMap takes the Engine's FuncMap and adds context-specific functions.
//
// The resulting FuncMap is only valid for the passed-in template.
func (e *Engine) alterFuncMap(t *template.Template) template.FuncMap {
// Clone the func map because we are adding context-specific functions.
var funcMap template.FuncMap = map[string]interface{}{}
for k, v := range e.FuncMap {
funcMap[k] = v
}
// Add the 'include' function here so we can close over t.
funcMap["include"] = func(name string, data interface{}) string {
buf := bytes.NewBuffer(nil)
if err := t.ExecuteTemplate(buf, name, data); err != nil {
buf.WriteString(err.Error())
}
return buf.String()
}
return funcMap
}