Getting Started
Request handling
- Routing
- Action Controller
- Resources
- Context
- Request Binding
- Middleware
- Error Handling
- Sessions
- Cookies
Frontend
Database
- Getting started with Pop
- Soda CLI
- Database Configuration
- Buffalo Integration
- Models
- Generators
- Migrations
- Fizz
- Mutations
- Querying
- Raw Queries
- Callbacks
- Scoping
- Associations and Relationships
- One to one associations
- One to many associations
Guides
- API Applications
- File Uploads
- Background Job Workers
- Mailers
- Tasks
- Plugins
- Local Authentication
- Third Party Authentication
- Events
- Go Modules
- Localization
- Logging
- Template Engines
- Testing
- Videos
Deploy
Partials
Frontend
Partials
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>
<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.