github.com/bketelsen/buffalo@v0.9.5/render/template.go (about)

     1  package render
     2  
     3  import (
     4  	"html"
     5  	"html/template"
     6  	"io"
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"github.com/pkg/errors"
    12  
    13  	// this blank import is here because dep doesn't
    14  	// handle transitive dependencies correctly
    15  	_ "github.com/russross/blackfriday"
    16  	"github.com/shurcooL/github_flavored_markdown"
    17  )
    18  
    19  type templateRenderer struct {
    20  	*Engine
    21  	contentType string
    22  	names       []string
    23  }
    24  
    25  func (s templateRenderer) ContentType() string {
    26  	return s.contentType
    27  }
    28  
    29  func (s templateRenderer) Render(w io.Writer, data Data) error {
    30  	var body template.HTML
    31  	var err error
    32  	for _, name := range s.names {
    33  		body, err = s.exec(name, data)
    34  		if err != nil {
    35  			return err
    36  		}
    37  		data["yield"] = body
    38  	}
    39  	w.Write([]byte(body))
    40  	return nil
    41  }
    42  
    43  func (s templateRenderer) partial(name string, dd Data) (template.HTML, error) {
    44  	d, f := filepath.Split(name)
    45  	name = filepath.Join(d, "_"+f)
    46  	return s.exec(name, dd)
    47  }
    48  
    49  func (s templateRenderer) exec(name string, data Data) (template.HTML, error) {
    50  	source, err := s.TemplatesBox.MustBytes(name)
    51  	if err != nil {
    52  		return "", err
    53  	}
    54  
    55  	helpers := map[string]interface{}{
    56  		"partial": s.partial,
    57  	}
    58  
    59  	helpers = s.addAssetsHelpers(helpers)
    60  
    61  	for k, v := range s.Helpers {
    62  		helpers[k] = v
    63  	}
    64  
    65  	if strings.ToLower(filepath.Ext(name)) == ".md" && strings.ToLower(s.contentType) != "text/plain" {
    66  		source = github_flavored_markdown.Markdown(source)
    67  		source = []byte(html.UnescapeString(string(source)))
    68  	}
    69  
    70  	body, err := s.TemplateEngine(string(source), data, helpers)
    71  	if err != nil {
    72  		return "", err
    73  	}
    74  
    75  	return template.HTML(body), nil
    76  }
    77  
    78  func (s templateRenderer) assetPath(file string) (string, error) {
    79  
    80  	if len(assetMap) == 0 || os.Getenv("GO_ENV") != "production" {
    81  		manifest, err := s.AssetsBox.MustString("manifest.json")
    82  
    83  		if err != nil {
    84  			return assetPathFor(file), nil
    85  		}
    86  
    87  		err = loadManifest(manifest)
    88  		if err != nil {
    89  			return assetPathFor(file), errors.Wrap(err, "your manifest.json is not correct")
    90  		}
    91  	}
    92  
    93  	return assetPathFor(file), nil
    94  }
    95  
    96  // Template renders the named files using the specified
    97  // content type and the github.com/gobuffalo/plush
    98  // package for templating. If more than 1 file is provided
    99  // the second file will be considered a "layout" file
   100  // and the first file will be the "content" file which will
   101  // be placed into the "layout" using "{{yield}}".
   102  func Template(c string, names ...string) Renderer {
   103  	e := New(Options{})
   104  	return e.Template(c, names...)
   105  }
   106  
   107  // Template renders the named files using the specified
   108  // content type and the github.com/gobuffalo/plush
   109  // package for templating. If more than 1 file is provided
   110  // the second file will be considered a "layout" file
   111  // and the first file will be the "content" file which will
   112  // be placed into the "layout" using "{{yield}}".
   113  func (e *Engine) Template(c string, names ...string) Renderer {
   114  	return templateRenderer{
   115  		Engine:      e,
   116  		contentType: c,
   117  		names:       names,
   118  	}
   119  }