Rendering

This document only applies when using https://github.com/gobuffalo/buffalo/render.
Please see github.com/gobuffalo/velvet for more details on the underlying templating package.


    Renderer Interface

    In order for a renderer to be able to be used with Context#Render it must implement the following interface:

    // Renderer interface that must be satisified to be used with
    // buffalo.Context.Render
    type Renderer interface {
    	ContentType() string
    	Render(io.Writer, Data) error
    }
    
    // Data type to be provided to the Render function on the
    // Renderer interface.
    type Data map[string]interface{}
    

    Thankfully the https://github.com/gobuffalo/buffalo/render [godoc] package implements that interface, and has a collection of useful render types already defined. It is recommended that you use this package, but feel free and write your own renderers!

    Creating a Render Engine

    A render engine is used to store information about configuration needed for rendering. Examples include: helpers, layouts, etc. Multiple engines can be initialized. For example one engine for the "main" site, and another for the "admin" portion.

    By default an initial render engine is created for the application during application generation:

    var r *render.Engine
    
    func init() {
    	r = render.New(render.Options{
    		HTMLLayout:     "application.html",
    		CacheTemplates: ENV == "production",
    		FileResolverFunc: func() resolvers.FileResolver {
    			return &resolvers.RiceBox{
    				Box: rice.MustFindBox("../templates"),
    			}
    		},
    	})
    }
    

    Markdown

    Files passed into the render.HTML or render.Template functions, that have an extension of .md, will be converted from Markdown (using GitHub flavored Markdown) to HTML before being run through the templating engine. This makes for incredibly easy templating for simpler pages.

    <!-- beatles.md -->
    # The Beatles
    
    {{#each names as |name|}}
    * {{name}}
    {{/each}}
    
    func Beatles(c buffalo.Context) error {
      c.Set("names", []string{"John", "Paul", "George", "Ringo"})
      return c.Render(200, r.HTML("beatles.md"))
    }
    
    <h1>The Beatles</h1>
    
    <ul>
      <li>John</li>
      <li>Paul</li>
      <li>George</li>
      <li>Ringo</li>
    </ul>