Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Added Storefront Pages guide

Damian Legawiec58602815ee49
diff --git a/docs/developer/storefront/pages.mdx b/docs/developer/storefront/pages.mdx
index 543f23c795..1d488fbba7 100644
--- a/docs/developer/storefront/pages.mdx
+++ b/docs/developer/storefront/pages.mdx
@@ -5,8 +5,135 @@ description: "Learn how to create and use pages in your Spree storefront"
## Built-in Pages
+Spree Storefront comes with a full set of built-in pages that you can customize via page builder.
+| Page | Description | Class | Controller |
+|------|-------------|-------|------------|
+| Home | The main landing page of your store | [Spree::Pages::Homepage](https://github.com/spree/spree/blob/main/core/app/models/spree/pages/homepage.rb) | Spree::HomeController |
+| Product Details | Individual product display page | [Spree::Pages::ProductDetails](https://github.com/spree/spree/blob/main/core/app/models/spree/pages/product_details.rb) | Spree::ProductsController `show` action |
+| Taxon | Shows products filtered by taxon | [Spree::Pages::Taxon](https://github.com/spree/spree/blob/main/core/app/models/spree/pages/taxon.rb) | Spree::TaxonsController |
+| Search Results | Displays search results for products | [Spree::Pages::SearchResults](https://github.com/spree/spree/blob/main/core/app/models/spree/pages/search_results.rb) | Spree::SearchController |
+| Shop All | Shows all products | [Spree::Pages::ShopAll](https://github.com/spree/spree/blob/main/core/app/models/spree/pages/shop_all.rb) | Spree::ProductsController `index` action |
+| Blog | Shows your store's blog posts and articles | [Spree::Pages::PostList](https://github.com/spree/spree/blob/main/core/app/models/spree/pages/post_list.rb) | Spree::PostsController `index` action |
+| Post | Individual blog post | [Spree::Pages::Post](https://github.com/spree/spree/blob/main/core/app/models/spree/pages/post.rb) | Spree::PostsController `show` action |
+| Password | Password page, when password protected storefront is enabled | [Spree::Pages::Password](https://github.com/spree/spree/blob/main/core/app/models/spree/pages/password.rb) | Spree::PasswordController |
+All of these page can be fully customized via page builder. You can add/remove sections, etc.
-## Custom Pages
+We're planning to add page builder support for the following pages in the next Spree releases:
+- Cart
+- Checkout
+- Account
+
+## Custom Landing Pages in the Admin
+
+You can easily create your own landing pages by going to to the `Admin -> Storefront -> Pages` and clicking on the `New Page` button. More on this in the [Create a Landing Page](/user/storefront/create-page) guide. Each custom landing page can be customized via page builder.
+
+![Landing Pages](/images/spree_admin_custom_landing_pages.png)
+
+Under the hood they use [Spree::Pages::Custom](https://github.com/spree/spree/blob/main/core/app/models/spree/pages/custom.rb) class.
+
+## Building a new Page from scratch
+
+If you need to build a page that is not one of the built-in pages, you can create your own page class. This will allow you to create a dedicated page for your business needs and at the same time allow your non-technical team to manage the page content without bugging you :)
+
+Let's assume you need to create a page that displays pickup locations. You already created a new model `Spree::PickupLocation` and added some data to the database. Now you want to create a page that displays these pickup locations.
+
+First, let's add a route for the new page.
+
+```ruby
+# config/routes.rb
+Spree::Core::Engine.add_routes do
+ scope '(:locale)', locale: /#{Spree.available_locales.join('|')}/, defaults: { locale: nil } do # this line will enable translations for the new page
+ resources :pickup_locations, only: [:index]
+ end
+end
+```
+
+Now, create the new page class file:
+
+```bash
+mkdir -p app/models/spree/pages && touch app/models/spree/pages/pickup_locations.rb
+```
+
+Now, let's add the new page class:
+
+```ruby
+module Spree
+ module Pages
+ class PickupLocations < Spree::Page
+ # use https://tabler.io/icons to find the icon name
+ def icon_name
+ 'map-marker-alt'
+ end
+
+ # This method is used to determine if the page is customizable via page builder
+ def customizable?
+ true
+ end
+
+ # this is the URL used to preview the page in the page builder UI
+ def page_builder_url
+ return unless page_builder_url_exists?(:pickup_locations_path)
+
+ Spree::Core::Engine.routes.url_helpers.pickup_locations_path
+ end
+
+ # this is the default sections that will be shown on the page
+ # in the later guide for sections you will learn how to add custom sections
+ def default_sections
+ [
+ Spree::PageSections::PageTitle.new
+ ]
+ end
+ end
+ end
+end
+```
+
+You will need to register the new page in the `config/initializers/spree.rb` file:
+
+```ruby
+
+```
+
+Now, the controller:
+
+```bash
+mkdir -p app/controllers/spree && touch app/controllers/spree/pickup_locations_controller.rb
+```
+
+```ruby
+# app/controllers/spree/pickup_locations_controller.rb
+module Spree
+ class PickupLocationsController < StoreController
+ def index
+ @current_page = current_theme.pages.find_by(type: 'Spree::Pages::PickupLocations') # this is very important, without this the page will not be found
+ @pickup_locations = current_store.pickup_locations
+ end
+ end
+end
+```
+
+Lastly we need to add a view file for the page:
+
+```bash
+mkdir -p app/views/spree/pickup_locations && touch app/views/spree/pickup_locations/index.html.erb
+```
+
+And add the following code to the view file:
+
+```erb
+<%= render_page(current_page, pickup_locations: @pickup_locations) %>
+```
+
+`render_page` is a helper method defined in the [Spree::PageHelper](https://github.com/spree/spree/blob/main/storefront/app/helpers/spree/page_helper.rb) module that renders the page. It fetches all page sections and renders them one by one in the order they are set in the page builder by store staff.
+
+Passing `pickup_locations` variable to the `render_page` method allows you to use it in the page sections templates, eg.
+
+```erb
+<%= pickup_locations.each do |pickup_location| %>
+ <%= pickup_location.name %>
+<% end %>
+```
diff --git a/docs/images/spree_admin_custom_landing_pages.png b/docs/images/spree_admin_custom_landing_pages.png
new file mode 100644
index 0000000000..dba22ed152
Binary files /dev/null and b/docs/images/spree_admin_custom_landing_pages.png differ
diff --git a/storefront/app/helpers/spree/page_helper.rb b/storefront/app/helpers/spree/page_helper.rb
index 10b90626ba..e0a67d97c4 100644
--- a/storefront/app/helpers/spree/page_helper.rb
+++ b/storefront/app/helpers/spree/page_helper.rb
@@ -1,5 +1,13 @@
module Spree
module PageHelper
+ # Renders the page with the current theme (or theme preview) and page preview if it exists.
+ # It fetches all page sections and renders them one by one in the order they are set in the page builder by store staff.
+ # It also handles lazy loading of sections.
+ #
+ # @param page [Spree::Page] the page to render
+ # @param variables [Hash] variables to pass to the page sections
+ # @option variables [Array] :pickup_locations ([]) the pickup locations to pass to the page sections
+ # @return [String] the rendered page
def render_page(page = nil, variables = {})
page ||= current_page
@@ -11,6 +19,12 @@ module Spree
"<main class='page-contents'>#{sections_html}</main>".html_safe
end
+ # Renders a single section of the page.
+ #
+ # @param section [Spree::PageSection] the section to render
+ # @param variables [Hash] variables to pass to the section
+ # @option variables [Boolean] :lazy_allowed (true) whether lazy loading is allowed for the section (if it supports it)
+ # @return [String] the rendered section
def render_section(section, variables = {}, lazy_allowed: true)
return '' if section.blank?
@@ -54,6 +68,7 @@ module Spree
''
end
+ # Renders a link to the page builder for the given link.
def page_builder_link_to(link, options = {}, &block)
if link.present?
link_to(spree_storefront_resource_url(link.linkable || link), options.except(:label)) do
diff --git a/storefront/app/helpers/spree/theme_helper.rb b/storefront/app/helpers/spree/theme_helper.rb
index abce5860c9..0ba87db972 100644
--- a/storefront/app/helpers/spree/theme_helper.rb
+++ b/storefront/app/helpers/spree/theme_helper.rb
@@ -1,9 +1,16 @@
module Spree
module ThemeHelper
+ # Returns the current page, if not found it will fallback to the homepage
+ #
+ # @return [Spree::Page] the current page
def current_page
@current_page ||= current_theme.pages.find_by(type: 'Spree::Pages::Homepage')
end
+ # Returns the current theme, if not found it will fallback to the default theme
+ # If `theme_id` is provided in the params, it will return the theme with the given id
+ #
+ # @return [Spree::Theme] the current theme
def current_theme
@current_theme ||= if params[:theme_id].present?
current_store.themes.find_by(id: params[:theme_id])
@@ -14,34 +21,56 @@ module Spree
@current_theme ||= current_store.themes.first
end
+ # Returns the current theme preview
+ #
+ # @return [Spree::ThemePreview] the current theme preview
def current_theme_preview
return if params[:theme_preview_id].blank?
@current_theme_preview ||= current_theme.previews.find_by(id: params[:theme_preview_id])
end
+ # Returns the current page preview
+ #
+ # @return [Spree::PagePreview] the current page preview
def current_page_preview
return if params[:page_preview_id].blank?
@current_page_preview ||= current_page.previews.find_by(id: params[:page_preview_id])
end
+ # Returns the current page or page preview, preview takes priority
+ #
+ # @return [Spree::Page] the current page or page preview
def current_page_or_preview
@current_page_or_preview ||= current_page_preview || current_page
end
+ # Returns the current theme or theme preview, preview takes priority
+ #
+ # @return [Spree::Theme] the current theme or theme preview
def current_theme_or_preview
@current_theme_or_preview ||= current_theme_preview || current_theme
end
+ # Returns the logo set in the `Spree::PageSections::Header` section
+ #
+ # @return [ActiveStorage::Attachment] the logo
def current_header_logo
@current_header_logo ||= current_theme_or_preview.sections.find_by(type: 'Spree::PageSections::Header')&.logo
end
+ # Returns whether the page builder is enabled
+ # It checks if there is a theme preview or page preview and if the `page_builder` param is set to `true`
+ #
+ # @return [Boolean] whether the page builder is enabled
def page_builder_enabled?
@page_builder_enabled ||= (current_theme_preview.present? || current_page_preview.present?) && params[:page_builder] == 'true'
end
+ # Returns the theme layout sections, eg. header, footer, etc.
+ #
+ # @return [Hash] the theme layout sections
def theme_layout_sections
@theme_layout_sections ||= current_theme_or_preview.sections.includes(:links, { asset_attachment: :blob },
{ blocks: [:rich_text_text, :links] }).all.each_with_object({}) do |section, hash|
@@ -55,6 +84,11 @@ module Spree
{}
end
+ # Returns the theme setting for the given name
+ # if preview is present, it will return the preview setting, otherwise it will return the theme setting
+ #
+ # @param name [String] the name of the theme setting
+ # @return [String] the theme setting
def theme_setting(name)
if current_theme_preview.present?
current_theme_preview.preferences.with_indifferent_access[name]
@@ -64,6 +98,9 @@ module Spree
end
# This helper allows us to specify opacity in Tailwind's color palette
+ #
+ # @param name [String] the name of the theme setting
+ # @return [String] the theme setting
def theme_setting_rgb_components(name)
hex_color = theme_setting(name)
return unless hex_color.present?
@@ -73,6 +110,10 @@ module Spree
end
# https://makandracards.com/makandra/496431-ruby-how-to-convert-hex-color-codes-to-rgb-or-rgba
+ # Converts a hex color to rgb
+ #
+ # @param hex [String] the hex color
+ # @return [String] the rgb color
def hex_color_to_rgb(hex)
return unless hex.present?
@@ -80,6 +121,10 @@ module Spree
"rgb(#{rgb.join(', ')})"
end
+ # Converts a hex color to rgba
+ #
+ # @param hex [String] the hex color
+ # @return [String] the rgba color
def hex_color_to_rgba(hex)
return unless hex.present?
@@ -88,6 +133,10 @@ module Spree
"rgba(#{rgb.join(', ')}, #{opacity.round(2)})"
end
+ # Returns the section inline CSS styles
+ #
+ # @param section [Spree::PageSection] the section
+ # @return [String] the section inline CSS styles
def section_styles(section)
styles = {}
@@ -116,6 +165,10 @@ module Spree
styles.map { |k, v| "#{k}: #{v}" }.join(';')
end
+ # Returns the section heading inline CSS styles
+ #
+ # @param section [Spree::PageSection] the section
+ # @return [String] the section heading inline CSS styles
def section_heading_styles(section)
styles = {}
@@ -128,6 +181,11 @@ module Spree
styles.compact_blank.map { |k, v| "#{k}: #{v}" }.join(';')
end
+ # Returns the block HTML attributes
+ # it automatically adds data attributes for page builder
+ #
+ # @param block [Spree::PageBlock] the block
+ # @return [Hash] the block attributes
def block_attributes(block, allowed_styles: :all)
has_width_desktop = block.respond_to?(:preferred_width_desktop) && block.preferred_width_desktop.present? ? "width-desktop='true'" : nil
@@ -147,6 +205,11 @@ module Spree
tag.attributes(attributes)
end
+ # Returns the link HTML attributes
+ # it automatically adds data attributes for page builder
+ #
+ # @param link [Spree::PageLink] the link
+ # @return [Hash] the link attributes
def link_attributes(link, as_html: true)
parent_type = case link.parent_type
when 'Spree::PageSection'
@@ -184,6 +247,11 @@ module Spree
end
end
+ # Returns the block inline CSS styles
+ #
+ # @param block [Spree::PageBlock] the block
+ # @param allowed_styles [Symbol] the allowed styles, if not provided, all styles will be returned
+ # @return [String] the block inline CSS styles
def block_styles(block, allowed_styles: :all)
styles = {}
@@ -229,12 +297,20 @@ module Spree
styles.map { |k, v| "#{k}: #{v}" }.join(';')
end
+ # Returns the block background color style
+ #
+ # @param block [Spree::PageBlock] the block
+ # @return [String] the block background color style
def block_background_color_style(block)
return nil unless block.respond_to?(:preferred_background_color) && block.preferred_background_color.present?
"background-color: #{block.preferred_background_color};"
end
+ # Returns the block CSS classes
+ #
+ # @param block [Spree::PageBlock] the block
+ # @return [String] the block CSS classes
def block_css_classes(block)
classes = []
classes << "justify-#{block.preferred_justify}" if block.respond_to?(:preferred_justify) && block.preferred_justify.present?