Module: Spina::Blocks::BlocksHelper

Defined in:
app/helpers/spina/blocks/blocks_helper.rb

Instance Method Summary collapse

Instance Method Details

#block_content(block, part_name = nil) ⇒ Object

Access a block’s content like page content Usage in block partial: block_content(block, :headline)



46
47
48
# File 'app/helpers/spina/blocks/blocks_helper.rb', line 46

def block_content(block, part_name = nil)
  block.content(part_name)
end

#block_has_content?(block, part_name) ⇒ Boolean

Check if a block has content for a given part

Returns:

  • (Boolean)


51
52
53
# File 'app/helpers/spina/blocks/blocks_helper.rb', line 51

def block_has_content?(block, part_name)
  block.has_content?(part_name)
end

#render_block(block, &content_block) ⇒ Object

Render a single block using its block_template partial. An optional content block is forwarded to the partial, so block templates can expose ‘<%= yield %>` slots for callers to inject markup. Usage:

<%= render_block(some_block) %>
<%= render_block(some_block) do %>...<% end %>


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/spina/blocks/blocks_helper.rb', line 26

def render_block(block, &content_block)
  return unless block&.active?

  current_spina_theme = Spina::Current.theme || current_theme
  theme_name = current_spina_theme.name.parameterize.underscore

  partial_path = "#{theme_name}/blocks/#{block.block_template}"

  if lookup_context.exists?(partial_path, [], true)
    # Use the string-form `render(string, locals, &block)` so passing a
    # content block doesn't trigger `render`'s implicit layout branch
    # (which would treat `:partial` as the wrapped content instead).
    render(partial_path, { block: block }, &content_block)
  else
    render_block_fallback(block)
  end
end

#render_blocks(page = nil) ⇒ Object

Render all blocks attached to the current page via PageBlocks Usage in a page template: <%= render_blocks %>



8
9
10
11
12
13
14
15
16
17
18
# File 'app/helpers/spina/blocks/blocks_helper.rb', line 8

def render_blocks(page = nil)
  page ||= current_page
  return unless page.respond_to?(:page_blocks)

  page.page_blocks.sorted.includes(:block).each do |page_block|
    block = page_block.block
    next unless block&.active?

    concat(render_block(block))
  end
end

#render_custom_block(key, &content_block) ⇒ Object

Render a custom (system) block by its unique key Usage:

<%= render_custom_block("header") %>
<%= render_custom_block("header") do %>...<% end %>


59
60
61
62
# File 'app/helpers/spina/blocks/blocks_helper.rb', line 59

def render_custom_block(key, &content_block)
  block = Spina::Blocks::Block.find_by(key: key)
  render_block(block, &content_block) if block
end