本文整理汇总了Golang中text/template.Template.ParseFiles方法的典型用法代码示例。如果您正苦于以下问题:Golang Template.ParseFiles方法的具体用法?Golang Template.ParseFiles怎么用?Golang Template.ParseFiles使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类text/template.Template
的用法示例。
在下文中一共展示了Template.ParseFiles方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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
}
示例3: generate
func (blankPanel blankPanel) generate(terminal, packageName string, employee Employee) []byte {
var t *template.Template
var buf bytes.Buffer
t = template.New("blankpanel.html")
t, err := t.ParseFiles("../lessgo/template/component/" + terminal + "/blankpanel.html")
if err != nil {
Log.Error(err.Error())
return []byte{}
}
data := make(map[string]interface{})
data["BlankPanel"] = blankPanel
data["Terminal"] = terminal
data["Employee"] = employee
err = t.Execute(&buf, data)
if err != nil {
Log.Error(err.Error())
return []byte{}
}
return buf.Bytes()
}
示例4: compileTemplates
// compileTemplates compile all templates for later use
func (b BlogBuilder) compileTemplates() error {
templates := [][]interface{}{
[]interface{}{"index", IndexTemplateName, true},
[]interface{}{"post", PostTemplateName, true},
[]interface{}{"rss", RSSTemplateName, false},
}
baseHTMLTemplate := filepath.Join(b.blogDir, LayoutsDirName, BaseTemplateName)
for _, t := range templates {
name := t[0].(string)
filename := t[1].(string)
isHTML := t[2].(bool)
templatePath := filepath.Join(b.blogDir, LayoutsDirName, filename)
var template *template.Template
var err error
if isHTML {
template, err = template.ParseFiles(baseHTMLTemplate, templatePath)
} else {
template, err = template.ParseFiles(templatePath)
}
if err != nil {
return fmt.Errorf("Fail to parse template %s due to %v", filename, err)
}
b.templates[name] = template
}
return nil
}
示例5: SendTo
func (s *_SmtpSender) SendTo(template_name string, subject string, receivers []mail.Address, args map[string]interface{}) (err error) {
var template *template.Template
var ok bool
if template, ok = s.Templates[template_name]; !ok {
if template, err = template.ParseFiles(filepath.Join(*s.Root, template_name)); err == nil {
s.Templates[template_name] = template
} else {
return
}
}
var c client
if *s.Ttl {
var config tls.Config
config.ServerName = *s.Server
if c, err = smtpoverttl.DialTTL(fmt.Sprintf("%s:%d", *s.Server, *s.Port), &config); err == nil {
return s.sendMail(c, subject, template, receivers, args)
}
} else {
if c, err = smtp.Dial(*s.Server); err == nil {
return s.sendMail(c, subject, template, receivers, args)
}
}
return
}
示例6: generate
func (tabFormPanel tabFormPanel) generate(terminal, packageName string, r *http.Request, employee Employee) []byte {
var t *template.Template
var buf bytes.Buffer
tabFormPanel.Id = packageName + "." + tabFormPanel.Id
runtimeComponentContain[tabFormPanel.Id] = tabFormPanel
t = template.New("tabformpanel.html")
t = t.Funcs(template.FuncMap{
"getComponentId": getComponentId,
"compareInt": CompareInt,
"compareString": CompareString,
"getPropValue": GetPropValue,
"dealHTMLEscaper": DealHTMLEscaper,
})
t, err := t.ParseFiles("../lessgo/template/component/" + terminal + "/tabformpanel.html")
if err != nil {
Log.Error(err.Error())
return []byte{}
}
if err != nil {
Log.Error(err.Error())
return []byte{}
}
data := make(map[string]interface{})
data["TabFormPanel"] = tabFormPanel
data["Terminal"] = terminal
data["Employee"] = employee
err = t.Execute(&buf, data)
if err != nil {
Log.Error(err.Error())
return []byte{}
}
return buf.Bytes()
}
示例7: generate
func (gridpanel gridPanel) generate(entity Entity, terminal, packageName string, employee Employee) []byte {
var t *template.Template
var buf bytes.Buffer
gridpanel.Id = packageName + "." + gridpanel.Id
runtimeComponentContain[gridpanel.Id] = gridpanel
t = template.New("gridpanel.html")
t = t.Funcs(template.FuncMap{
"getComponentId": getComponentId,
"compareInt": CompareInt,
"compareString": CompareString,
})
t, err := t.ParseFiles("../lessgo/template/component/" + terminal + "/gridpanel.html")
if err != nil {
Log.Error(err.Error())
return []byte{}
}
data := make(map[string]interface{})
data["Gridpanel"] = gridpanel
data["Entity"] = entity
data["Terminal"] = terminal
data["SearchLength"] = len(gridpanel.Searchs)
data["ActionLength"] = len(gridpanel.Actions)
data["Employee"] = employee
err = t.Execute(&buf, data)
if err != nil {
Log.Error(err.Error())
return []byte{}
}
return buf.Bytes()
}
示例8: GenerateViewport
//扩展viewport的同时,记得同时扩展container
func (viewport Viewport) GenerateViewport(terminal, packageName string, r *http.Request) []byte {
content := ""
for _, gridpanel := range viewport.GridPanels {
content += string(gridpanel.GenerateGridPanel(GetEntity(gridpanel.Entity), terminal, packageName))
}
for _, formpanel := range viewport.FormPanels {
content += string(formpanel.GenerateFormPanel(GetEntity(formpanel.Entity), terminal, packageName, r))
}
for _, mutiformpanel := range viewport.MutiFormPanels {
content += string(mutiformpanel.GenerateMutiFormPanel(terminal, packageName, r))
}
var t *template.Template
var buf bytes.Buffer
t = template.New("viewport.html")
t, err := t.ParseFiles("../lessgo/template/component/"+terminal+"/viewport.html", "../lessgo/template/component/"+terminal+"/nav.html")
if err != nil {
Log.Error(err.Error())
return []byte{}
}
data := make(map[string]interface{})
data["Content"] = content
data["Nav"] = navs
err = t.Execute(&buf, data)
if err != nil {
Log.Error(err.Error())
return []byte{}
}
return buf.Bytes()
}
示例9: renderTemplate
func renderTemplate(viewName string, model interface{}, w http.ResponseWriter, isPartial bool) {
var (
t *template.Template
err error
exists bool
)
//Caso não exista o template no cache, vamos prepará-lo
if t, exists = templateCache[viewName]; !exists || DebugMode {
t = template.New("").Funcs(helpersFuncs)
t, err = t.ParseFiles(filepath.Join(TemplatesDir, mainLayoutFile), filepath.Join(TemplatesDir, viewName+".html"))
if err != nil {
log.Println(err.Error())
InternalError(err, w)
return
}
templateCache[viewName] = t
if DebugMode {
log.Println(viewName, " compilada")
}
}
w.Header().Set("Content-Type", "text/html")
if !isPartial {
err = t.ExecuteTemplate(w, "LAYOUT", viewContext{model})
} else {
err = t.ExecuteTemplate(w, "MAIN", viewContext{model})
}
if err != nil {
InternalError(err, w)
}
}
示例10: main
//.........这里部分代码省略.........
root := FlatTypeRoot{
Request: &req,
Visible: getFlatTypes(&req, false, nil),
Exported: getFlatTypes(&req, true, nil),
Params: params,
HasData: false,
ExecParam: nil,
}
left, right := "(*", "*)"
if p := params.Get("left"); len(p) > 0 {
left = p
}
if p := params.Get("right"); len(p) > 0 {
right = p
}
// This code is all awful but at least it gets the job done right now.
var tx *template.Template
funcs := template.FuncMap{
"find": (typeFinder{root.Request}).Find,
"fexec": func(name, outfile string, data ...interface{}) error {
subroot := root
if len(data) == 1 {
d := data[0]
if _, ok := d.(FlatTypeRoot); !ok {
subroot.ExecParam = d
}
} else if len(data) > 1 {
subroot.ExecParam = data
}
var out io.Writer = ioutil.Discard
if len(name) > 0 {
b, ok := files[outfile]
if !ok {
b = &bytes.Buffer{}
files[outfile] = b
}
out = b
subroot.HasData = b.Len() > 0
}
if name != "" {
return tx.ExecuteTemplate(out, name, subroot)
} else {
return tx.Execute(out, subroot)
}
},
"exec": func(name string, dot ...interface{}) (string, error) {
var data interface{} = dot
if len(dot) == 1 {
data = dot[0]
} else if len(dot) == 0 {
data = nil
}
var buf bytes.Buffer
var err error
if name != "" {
err = tx.ExecuteTemplate(&buf, name, data)
} else {
err = tx.Execute(&buf, data)
}
return buf.String(), err
},
}
tx = template.New("").Delims(left, right).Funcs(mergeTypeChecks(copyDefaultTemplateFuncs(funcs)))
tx, err := tx.ParseFiles(params["template"]...)
if err != nil {
resp.Error = heapString("error parsing template(s): " + err.Error())
return
}
templates := params["template"]
if tn := params["exec"]; len(tn) > 0 {
templates = tn
}
for _, name := range templates {
if err := tx.ExecuteTemplate(ioutil.Discard, name, root); err != nil {
resp.Error = heapString(err.Error())
return
}
}
for name, buf := range files {
f := &compiler.CodeGeneratorResponse_File{
Name: heapString(name),
Content: heapString(buf.String()),
}
log.Printf("OUT=%q", name)
resp.File = append(resp.File, f)
}
}
示例11: generate
func (formpanel formPanel) generate(entity Entity, terminal, packageName string, r *http.Request, employee Employee) []byte {
var t *template.Template
var buf bytes.Buffer
formpanel.Id = packageName + "." + formpanel.Id
runtimeComponentContain[formpanel.Id] = formpanel
t = template.New("formpanel.html")
t = t.Funcs(template.FuncMap{
"getComponentId": getComponentId,
"compareInt": CompareInt,
"compareString": CompareString,
"getPropValue": GetPropValue,
"dealHTMLEscaper": DealHTMLEscaper,
})
t, err := t.ParseFiles("../lessgo/template/component/" + terminal + "/formpanel.html")
if err != nil {
Log.Error(err.Error())
return []byte{}
}
if err != nil {
Log.Error(err.Error())
return []byte{}
}
data := make(map[string]interface{})
data["Formpanel"] = formpanel
data["Entity"] = entity
data["Terminal"] = terminal
data["Employee"] = employee
if formpanel.Load == "true" {
id := r.FormValue("id")
model, err := findById(entity, id)
if err != nil {
Log.Error(err.Error())
return []byte{}
}
data["Model"] = model
}
err = t.Execute(&buf, data)
if err != nil {
Log.Error(err.Error())
return []byte{}
}
return buf.Bytes()
}
示例12: generateViewport
//扩展viewport的同时,记得同时扩展container
func (viewport viewport) generateViewport(terminal, packageName string, r *http.Request, employee Employee) []byte {
content := ""
for _, formpanel := range viewport.FormPanels {
content += string(formpanel.generate(getEntity(formpanel.Entity), terminal, packageName, r, employee))
}
for _, tabFormPanel := range viewport.TabFormPanels {
content += string(tabFormPanel.generate(terminal, packageName, r, employee))
}
for _, gridpanel := range viewport.GridPanels {
content += string(gridpanel.generate(getEntity(gridpanel.Entity), terminal, packageName, employee))
}
for _, customgridpanel := range viewport.CustomGridPanels {
content += string(customgridpanel.generate(terminal, packageName, employee))
}
for _, customformpanel := range viewport.CustomFormPanels {
content += string(customformpanel.generate(terminal, packageName, employee))
}
for _, blankpanel := range viewport.BlankPanels {
content += string(blankpanel.generate(terminal, packageName, employee))
}
data := make(map[string]interface{})
data["Content"] = content
data["Crumbs"] = viewport.Crumbs
data["Employee"] = employee
data["SiteName"] = SiteName
data["SiteIcon"] = SiteIcon
data["CustomJs"] = viewport.CustomJs
var t *template.Template
var buf bytes.Buffer
tempName := ""
if viewport.Window == "true" {
t = template.New("window.html")
data["ParentComponentId"] = r.FormValue("parentComponentId")
data["ParentWindowName"] = r.FormValue("parentWindowName")
tempName = "../lessgo/template/component/" + terminal + "/window.html"
} else {
t = template.New("viewport.html")
tempName = "../lessgo/template/component/" + terminal + "/viewport.html"
}
t = t.Funcs(template.FuncMap{
"compareString": CompareString,
})
t, err := t.ParseFiles(tempName)
if err != nil {
Log.Error(err.Error())
return []byte{}
}
err = t.Execute(&buf, data)
if err != nil {
Log.Error(err.Error())
return []byte{}
}
return buf.Bytes()
}