Partials

This document only applies when using https://github.com/gobuffalo/buffalo/render.
Please see github.com/gobuffalo/plush 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.

<h1>Create New User</h1>

<%= partial("users/form.html") %>
<h1>Create New User</h1>

<form action="/users">
<!-- form stuff here  -->
<form>
<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)

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"))
}
<h1>Edit <%= user.Name %> (<%= user.ID %>)</h1>

<%= partial("users/form.html") %>
<form action="/users/<%= user.ID %>">
<!-- form stuff here  -->
</form>
<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.

func UsersIndex(c buffalo.Context) error {
  c.Set("users", []string{"John Lennon", "Paul McCartney", "George Harrison", "Ringo Starr"})
  return c.Render(r.HTML("users/index.html"))
}
<h1>All Users</h1>

<ul>
  <%= for (u) in users { %>
    <%= partial("users/user.html", {user: u}) %>
  <% } %>
</ul>
<li><%= user.Name %></li>
<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.