Module: Spree::PageHelper

Defined in:
app/helpers/spree/page_helper.rb

Instance Method Summary collapse

Instance Method Details

#layout_sectionsArray

Returns the layout sections of the page with current theme or theme preview.

Returns:

  • (Array)

    the layout sections of the page



78
79
80
# File 'app/helpers/spree/page_helper.rb', line 78

def layout_sections
  @layout_sections ||= current_theme_or_preview.sections
end

Renders a link to the page builder for the given link.



110
111
112
113
114
115
116
117
118
# File 'app/helpers/spree/page_helper.rb', line 110

def page_builder_link_to(link, options = {}, &block)
  if link.present?
    link_to(spree_storefront_resource_url(link.linkable || link), options.except(:label)) do
      block.present? ? block.call : options[:label]
    end
  else
    block&.call&.html_safe
  end
end

Renders the footer sections of the page.

Returns:

  • (String)

    the rendered footer sections



94
95
96
97
98
# File 'app/helpers/spree/page_helper.rb', line 94

def render_footer_sections
  @render_footer_sections ||= layout_sections.find_all { |section| section.role == 'footer' }.map do |section|
    render_section(section)
  end.join.html_safe
end

#render_header_sectionsString

Renders the header sections of the page.

Returns:

  • (String)

    the rendered header sections



85
86
87
88
89
# File 'app/helpers/spree/page_helper.rb', line 85

def render_header_sections
  @render_header_sections ||= layout_sections.find_all { |section| section.role == 'header' }.map do |section|
    render_section(section)
  end.join.html_safe
end

#render_page(page = nil, variables = {}) ⇒ String

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.

Parameters:

  • page (Spree::Page) (defaults to: nil)

    the page to render

  • variables (Hash) (defaults to: {})

    variables to pass to the page sections

Options Hash (variables):

  • :pickup_locations (Array) — default: []

    the pickup locations to pass to the page sections

Returns:

  • (String)

    the rendered page



11
12
13
14
15
16
17
18
19
20
# File 'app/helpers/spree/page_helper.rb', line 11

def render_page(page = nil, variables = {})
  page ||= current_page

  sections = current_page_preview.present? ? current_page_preview.sections : page.sections
  sections_html = sections.includes(section_includes).preload_associations_lazily.map do |section|
    render_section(section, variables)
  end.join.html_safe

  "<main class='page-contents'>#{sections_html}</main>".html_safe
end

#render_section(section, variables = {}, lazy_allowed: true) ⇒ String

Renders a single section of the page.

Parameters:

  • section (Spree::PageSection)

    the section to render

  • variables (Hash) (defaults to: {})

    variables to pass to the section

Options Hash (variables):

  • :lazy_allowed (Boolean) — default: true

    whether lazy loading is allowed for the section (if it supports it)

Returns:

  • (String)

    the rendered section



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/spree/page_helper.rb', line 28

def render_section(section, variables = {}, lazy_allowed: true)
  return '' if section.blank?

  hidden = section.can_be_hidden? && !section.preferred_visible
  return '' if hidden && !page_builder_enabled?

  variables[:section] = section
  variables[:loaded] = true

  css_id = "section-#{section.id}"
  css_class = "section-#{section.class.name.demodulize.underscore.dasherize}"

  if page_builder_enabled?
    turbo_frame_tag(css_id, class: css_class) do
      (:div,
        data: {
          editor_id: css_id,
          editor_name: section.name,
          editor_link: spree.edit_admin_page_section_path(section)
        },
        style: (hidden ? 'opacity: 0.4;' : nil)
      ) do
        render('/' + section.to_partial_path, **variables)
      end
    end
  elsif section.lazy? && lazy_allowed
    variables[:loaded] = false
    variables[:url_options] = { locale: I18n.locale }

    path = section.lazy_path(variables)

    turbo_frame_tag(css_id, src: path, loading: :lazy, class: css_class, data: { controller: 'prefetch-lazy', turbo_permanent: true }) do
      render('/' + section.to_partial_path, **variables)
    end
  else
    (:div, id: css_id, class: css_class) do
      render('/' + section.to_partial_path, **variables)
    end
  end
rescue ActionView::MissingTemplate, ActionView::Template::Error => e
  raise e unless Rails.env.production?

  Rails.error.report(e, context: { section_id: section.id }, source: 'spree.storefront')

  ''
end

#section_includesArray

Returns the includes for the section.

Returns:

  • (Array)

    the includes for the section



103
104
105
106
107
# File 'app/helpers/spree/page_helper.rb', line 103

def section_includes
  [
    { asset_attachment: :blob }
  ]
end