github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/render/render.go (about)

     1  package render
     2  
     3  import (
     4  	"github.com/gobuffalo/helpers"
     5  	"github.com/gobuffalo/helpers/forms"
     6  	"github.com/gobuffalo/helpers/forms/bootstrap"
     7  	"github.com/gobuffalo/plush/v4"
     8  )
     9  
    10  // Engine used to power all defined renderers.
    11  // This allows you to configure the system to your
    12  // preferred settings, instead of just getting
    13  // the defaults.
    14  type Engine struct {
    15  	Options
    16  }
    17  
    18  // New render.Engine ready to go with your Options
    19  // and some defaults we think you might like.
    20  func New(opts Options) *Engine {
    21  	if opts.Helpers == nil {
    22  		opts.Helpers = Helpers{}
    23  	}
    24  
    25  	if len(opts.Helpers) == 0 {
    26  		opts.Helpers = defaultHelpers()
    27  	}
    28  
    29  	if opts.TemplateEngines == nil {
    30  		opts.TemplateEngines = map[string]TemplateEngine{}
    31  	}
    32  	if _, ok := opts.TemplateEngines["html"]; !ok {
    33  		opts.TemplateEngines["html"] = plush.BuffaloRenderer
    34  	}
    35  	if _, ok := opts.TemplateEngines["plush"]; !ok {
    36  		opts.TemplateEngines["plush"] = plush.BuffaloRenderer
    37  	}
    38  	if _, ok := opts.TemplateEngines["text"]; !ok {
    39  		opts.TemplateEngines["text"] = plush.BuffaloRenderer
    40  	}
    41  	if _, ok := opts.TemplateEngines["txt"]; !ok {
    42  		opts.TemplateEngines["txt"] = plush.BuffaloRenderer
    43  	}
    44  	if _, ok := opts.TemplateEngines["js"]; !ok {
    45  		opts.TemplateEngines["js"] = plush.BuffaloRenderer
    46  	}
    47  	if _, ok := opts.TemplateEngines["md"]; !ok {
    48  		opts.TemplateEngines["md"] = MDTemplateEngine
    49  	}
    50  	if _, ok := opts.TemplateEngines["tmpl"]; !ok {
    51  		opts.TemplateEngines["tmpl"] = GoTemplateEngine
    52  	}
    53  
    54  	if opts.DefaultContentType == "" {
    55  		opts.DefaultContentType = "text/html; charset=utf-8"
    56  	}
    57  
    58  	e := &Engine{
    59  		Options: opts,
    60  	}
    61  	return e
    62  }
    63  
    64  func defaultHelpers() Helpers {
    65  	h := Helpers(helpers.ALL())
    66  	h[forms.FormKey] = bootstrap.Form
    67  	h[forms.FormForKey] = bootstrap.FormFor
    68  	h["form_for"] = bootstrap.FormFor
    69  	return h
    70  }