logo Buffalo slack logo
Partials
Frontend

Partials#

This document only applies when using https://github.com/gobuffalo/buffalo/tree/main/render. Please see github.com/gobuffalo/plush for more details on the underlying templating package.

Usage#

You can call your partials using partial plush helper:

templates/users/form.plush.html
templates/users/new.plush.html
Output
<form action="/users/" method="POST">
<!-- form content 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
templates/users/edit.plush.html
templates/users/form.plush.html
Output
func UsersEdit(c buffalo.Context) error {
	user := User{
		Name: "John Smith",
	}
	// ...
	c.Set("user", user)
	return c.Render(http.StatusOK, render.HTML("users/edit.plush.html"))
}

Local Context#

In addition to have the global context, you can set additional variable only for partials as “local” variables.

actions/colors.go
templates/colors/index.plush.html
templates/colors/details.plush.html
Output
func ColorsHandler(c buffalo.Context) error {
  colors := map[string]interface{}{
		"White":  "#FFFFFF",
		"Maroon": "#800000",
		"Red":    "#FF0000",
		"Purple": "#800080",
	}

	c.Set("colors", colors)
	return c.Render(http.StatusOK, r.HTML("colors/index.plush.html"))
}

Helpers#

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