Skip to main content

Quick Start

Sections are the building blocks of all pages in Spree Storefront. There are two types of sections:

Layout sections

  • eg. Header, Footer
  • Present on all pages

Content sections

  • eg. Hero, Featured Products
  • Present on specific pages
  • can be managed via the Page Builder
Let’s take a look at the page structure of Spree Storefront: As you can see, the page is divided into sections. Each section is a component that is used to create the page. Most sections are containers that consist of multiple blocks which you can manage via the Page Builder (add/customize/remove/change their order). Each section consists of:

Layout Sections

We have two types of layout sections - Header and Footer. As the name suggests, they are rendered at the top and bottom of the page respectively. Between them, we have a main content area that is used to render the page sections.

Header sections

Announcement Bar

A simple announcement bar section that displays a message to the users.
Header is one of the most important sections in the storefront. It is used to display the store’s logo, navigation menu, search bar, cart icon, and user menu. Header can have a simple one-level navigation menu or a more complex multi-level menu (aka mega menu)

Newsletter

A newsletter section that allows users to subscribe to the store’s newsletter. If you connected your store to a newsletter provider (eg. Klaviyo, Mailchimp, etc.), you can use this section to collect emails and send them to your provider (Only in Spree 5.1+).
Footer is a section that is used to display the store’s footer. It is typically used to display the store’s logo, copyright information, store policies, and other important links.

Content Sections

Documentation on content sections is coming soon

Architecture

Let’s dive into the details of how sections work.

Active Record Model

Each section’s model inherit from Spree::PageSection abstract model class.

Associations

Each sections has many blocks and links. You can call them via section.blocks and section.links respectively. Section belongs to a polymorphic parent model called pageable which can be either Spree::Page (content sections) or Spree::Theme (layout sections). You can access section’s theme by calling section.theme.

Preferences

Each section has set of default preferences Particular sections can introduce their own preferences. For example, Spree::PageSections::ImageWithText has desktop_image_alignment and vertical_alignment preferences.

Creating a New Section

This guide walks you through creating a custom section for your Spree storefront. We’ll create a “Testimonials” section that displays customer reviews.

Step 1: Create the Model

Create your section model that inherits from Spree::PageSection. Place it in app/models/spree/page_sections/:
app/models/spree/page_sections/testimonials.rb

Step 2: Register the Section

Add your section to Spree.page_builder.page_sections in your Spree initializer:
config/initializers/spree.rb
Sections are filtered by their role method. By default, sections have role 'content' which makes them available in the Page Builder. Sections with role 'header' or 'footer' are layout sections.

Step 3: Create the Admin Form

Create the admin form partial for configuring the section in the Page Builder. Place it in app/views/spree/admin/page_sections/forms/:
app/views/spree/admin/page_sections/forms/_testimonials.html.erb
The data: { action: 'auto-submit#submit' } attribute enables live preview in the Page Builder - changes are saved automatically as the user types.

Step 4: Create the Storefront View

Create the storefront partial that renders the section. Place it in your theme’s views directory:
app/views/themes/default/spree/page_sections/_testimonials.html.erb

Section Roles

Sections have different roles that determine where they appear: To set a section’s role, override the self.role class method:

Section with Image Upload

If your section needs an image, use the built-in asset attachment:
app/models/spree/page_sections/hero_banner.rb
Admin form with image upload:
app/views/spree/admin/page_sections/forms/_hero_banner.html.erb
Display in storefront:
To add clickable links to your section, include the Spree::HasPageLinks concern (included by default) and define links:

Lazy Loading Sections

For sections that load external data (products, taxons), implement lazy loading to improve page performance:

Helper Methods

These helper methods are available in storefront section views:

Adding Translations

Add translations for your section in your locale files:
config/locales/en.yml

Complete Example

Here’s a complete example putting it all together - a “Brand Story” section:
1

Create the model

app/models/spree/page_sections/brand_story.rb
2

Register the section

config/initializers/spree.rb
3

Create admin form

app/views/spree/admin/page_sections/forms/_brand_story.html.erb
4

Create storefront view

app/views/themes/default/spree/page_sections/_brand_story.html.erb