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.
// 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.hml
<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.
// actions/users.go
func UsersIndex(c buffalo.Context) error {
c.Set("users", []string{"John Lennon", "Paul McCartney", "George Harrison", "Ringo Starr"})
return c.Render(200, r.HTML("users/index.html"))
}
// templates/users/index.html
<h1>All Users</h1>
<ul>
<%= for (u) in users { %>
<%= partial("users/user.html", {user: u}) %>
<% } %>
</ul>
// templates/users/_user.html
<li><%= user.Name %></li>
// output
<h1>All Users</h1>
<ul>
<li>John Lennon
<li>Paul McCartney
<li>George Harrison
<li>Ringo Starr
</ul>
Helpers
Partials are not much different from standard templates in Buffalo. They include all of the same helpers as well.
Feedback
If you found an error or something which needs to be improved, and you want to contribute to fix it:
If you'd like to suggest something to improve this page: