Partials

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.


    Naming

    All partial file names must start with an _. For example: _form.html. This helps to differentiate partials from other view templates in your application.

    templates/users/new.html

    <h1>Create New User</h1>
    
    {{ partial "users/form.html" }}
    

    templates/users/_form.html

    <form action="/users">
    <!-- form stuff here  -->
    </form>
    

    output

    <h1>Create New User</h1>
    
    <form action="/users">
    <!-- form stuff here  -->
    </form>
    

    Context

    All rendering context from the parent template will automatically pass through to the partial, and any partials that partial may call. (see also context)

    actions/users.go

    func UsersEdit(c buffalo.Context) error {
      // do some work to find the user
      c.Set("user", user)
      return c.Render(200, render.HTML("users/edit.html"))
    }
    

    templates/users/edit.html

    <h1>Edit {{user.Name}} ({{user.ID}})</h1>
    
    {{ partial "users/form.html" }}
    

    templates/users/_form.html

    <form action="/users/{{user.ID}}">
    <!-- form stuff here  -->
    </form>
    

    output

    <h1>Edit Mark Bates (1)</h1>
    
    <form action="/users/1">
    <!-- form stuff here  -->
    </form>
    

    "Local" Context

    In addition to have the context of the parent template, partials can also be sent additional information as "local" variables.

    templates/users/index.html

    <h1>All Users</h1>
    
    <ul>
      {{#each users as |user|}}
        {{ partial "users/user.html" user=user }}
      {{/each}}
    </ul>
    

    templates/users/_user.html

    <li>{{user.Name}}</li>
    

    outputs

    <h1>All Users</h1>
    
    <ul>
      <li>John Lennon</li>
      <li>Paul McCartney</li>
      <li>George Harrison</li>
      <li>Ringo Starr</li>
    </ul>
    

    Helpers

    Partials are not much different from standard templates in Buffalo. They include all of the same helpers as well.