docs/developer/storefront/rails/helper-methods.mdx
import SpreeTheme from '/snippets/objects/spree_theme.mdx' import SpreeOrder from '/snippets/objects/spree_order.mdx' import SpreeStore from '/snippets/objects/spree_store.mdx' import SpreeUser from '/snippets/objects/spree_user.mdx'
Here you can find a list of helper methods provided by Spree that are available in the Storefront. These methods can be used in every template.
current_storeReturns the current store Spree::Store instance.
current_themeReturns the current theme Spree::Theme instance.
current_pageReturns the current Spree::Page instance for the current route.
<%= current_page.name %>
current_orderReturns the current order object. If no order is found, it will return nil.
try_spree_current_userReturns the current user object (class depends on the Spree.user_class configuration). If the user is not signed in, it will return nil.
<%= try_spree_current_user.present? %>
current_currencyReturns the currently selected currency. By default in the Storefront this will be store.default_currency. This can be changed in the Settings -> Store Defaults page.
<%= current_currency %>
will return
USD
current_localeReturns the currently selected locale. By default in the Storefront this will be store.default_locale. This can be changed in the Settings -> Store Defaults page. If there are multiple locales available, it will return the locale that is currently selected by the user.
<%= current_locale %>
current_taxonReturns the current taxon when viewing a category/collection page.
<%= current_taxon&.name %>
current_wishlistReturns the current user's default wishlist for the current store.
<% if current_wishlist.present? %>
<%= current_wishlist.wished_items.count %> items
<% end %>
render_pageRenders a page with all its sections. This is the main method for rendering page content.
<%= render_page(current_page, pickup_locations: @pickup_locations) %>
Parameters:
page - The page to render (defaults to current_page)variables - Hash of variables to pass to section templatesrender_sectionRenders a single section.
<%= render_section(section, product: @product) %>
render_header_sectionsRenders all header sections (announcement bar, header).
<%= render_header_sections %>
render_footer_sectionsRenders all footer sections (newsletter, footer).
<%= render_footer_sections %>
section_stylesReturns inline CSS styles for a section based on its preferences (padding, colors, borders).
<div style="<%= section_styles(section) %>">
<!-- Section content -->
</div>
block_attributesReturns data attributes for a block (used for Page Builder editing).
<h2 <%= block_attributes(block) %>>
<%= block.text %>
</h2>
page_builder_enabled?Returns true when the page is being viewed in Page Builder preview mode.
<% cache_unless page_builder_enabled?, cache_key do %>
<!-- Cached content -->
<% end %>
page_builder_link_toRenders a link from a Spree::PageLink object.
<%= page_builder_link_to section.link, class: 'btn-primary' %>
<%# With custom content %>
<%= page_builder_link_to section.link do %>
<span class="icon">→</span> Click here
<% end %>
supported_currenciesReturns the list of supported currencies for the current store as an array of strings.
<%= supported_currencies %>
will return
["USD", "EUR"]
display_priceDisplays a formatted price with currency symbol.
<%= display_price(product.price_in(current_currency)) %>
local_timeDisplays a time in the user's timezone in a human readable format (based on the browser's timezone).
<%= local_time(order.sent_to_erp_at) %>
Provided by local_time gem.
spree_storefront_resource_urlGenerates a URL for a Spree resource (product, taxon, post, page).
<%= link_to product.name, spree_storefront_resource_url(product) %>
<%= link_to taxon.name, spree_storefront_resource_url(taxon) %>
spree.nested_taxons_pathGenerates a URL for a taxon using its full permalink.
<%= link_to taxon.name, spree.nested_taxons_path(taxon) %>
<%# => /t/categories/clothing/shirts %>
spree_image_tagRenders an optimized image tag with automatic WebP conversion and retina support.
<%= spree_image_tag product.images.first,
width: 400,
height: 400,
alt: product.name,
loading: :lazy %>
spree_image_urlGenerates a URL for an image with specified dimensions.
<%= spree_image_url(product.images.first, width: 800, height: 600) %>
taxon_productsReturns products for a given taxon, useful in section templates.
<% taxon_products(section.taxon).limit(4).each do |product| %>
<%= render 'spree/products/card', product: product %>
<% end %>
storefront_productsReturns products based on current filters and sorting.
<% storefront_products.each do |product| %>
<%= render 'spree/products/card', product: product %>
<% end %>
spree_storefront_base_cache_keyReturns the base cache key for storefront caching.
<% cache [spree_storefront_base_cache_key, product] do %>
<%= render 'product_card', product: product %>
<% end %>
spree_storefront_base_cache_scopeReturns a cache scope proc for section caching.
<% cache_unless page_builder_enabled?, spree_storefront_base_cache_scope.call(section) do %>
<!-- Section content -->
<% end %>
These helpers are used for Page Builder preview functionality:
| Helper | Description |
|---|---|
current_theme_preview | Returns the theme preview if in preview mode |
current_page_preview | Returns the page preview if in preview mode |
current_theme_or_preview | Returns theme preview or active theme |
current_page_or_preview | Returns page preview or active page |