Templating

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.


    General Usage

    If you know handlebars, you basically know how to use Velvet.

    Let's assume you have a template (a string of some kind):

    <!-- templates/index.html -->
    <h1>{{ name }}</h1>
    <ul>
      {{#each names}}
        <li>{{ @value }}</li>
      {{/each}}
    </ul>
    
    {{! "this is a comment and won't get printed" }}
    

    Given that string, you can render the template like such:

    func IndexHandler(c buffalo.Context) error {
      c.Set("names", []string{"John", "Paul", "George", "Ringo"})
      return c.Render(200, r.HTML("index.html"))
    }
    

    Which would result in the following output:

    <h1>Mark</h1>
    <ul>
      <li>John</li>
      <li>Paul</li>
      <li>George</li>
      <li>Ringo</li>
    </ul>
    

    If Statements

    What to do? Should you render the content, or not? Using Velvet's built in if, else, and unless helpers let you figure it out for yourself.

    {{#if true }}
      render this
    {{/if}}
    

    Else Statements

    {{#if false }}
      won't render this
    {{ else }}
      render this
    {{/if}}
    

    Unless Statements

    {{#unless true }}
      won't render this
    {{/unless}}