github.com/jacobsoderblom/buffalo@v0.11.0/render/template_engine.go (about)

     1  package render
     2  
     3  import (
     4  	"bytes"
     5  	"html/template"
     6  )
     7  
     8  // TemplateEngine needs to be implemented for a template system to be able to be used with Buffalo.
     9  type TemplateEngine func(input string, data map[string]interface{}, helpers map[string]interface{}) (string, error)
    10  
    11  // GoTemplateEngine implements the TemplateEngine interface for using standard Go templates
    12  func GoTemplateEngine(input string, data map[string]interface{}, helpers map[string]interface{}) (string, error) {
    13  	// since go templates don't have the concept of an optional map argument like Plush does
    14  	// add this "null" map so it can be used in templates like this:
    15  	// {{ partial "flash.html" .nilOpts }}
    16  	data["nilOpts"] = map[string]interface{}{}
    17  
    18  	t := template.New(input)
    19  	if helpers != nil {
    20  		t = t.Funcs(helpers)
    21  	}
    22  
    23  	t, err := t.Parse(input)
    24  	if err != nil {
    25  		return "", err
    26  	}
    27  
    28  	bb := &bytes.Buffer{}
    29  	err = t.Execute(bb, data)
    30  	return bb.String(), err
    31  }