Sayfa.Behaviours.Block behaviour (Sayfa v0.5.0)

Copy Markdown View Source

Behaviour for defining reusable template blocks.

A block is a reusable EEx component that renders HTML. Blocks are invoked in templates via the @block helper function:

<%= @block.(:recent_content, limit: 5) %>
<%= @block.(:toc) %>

Assigns

The render/1 callback receives a map of assigns that always includes:

  • :site — the resolved site config map
  • :content — the current Sayfa.Content struct (may be nil for list pages)
  • :contents — list of all site contents (for blocks like recent_content)
  • :lang — the current language atom

Plus any additional options passed by the template caller.

Examples

defmodule MyApp.Blocks.Banner do
  @behaviour Sayfa.Behaviours.Block

  @impl true
  def name, do: :banner

  @impl true
  def render(assigns) do
    text = Map.get(assigns, :text, "Welcome!")
    ~s(<div class="banner">#{text}</div>)
  end
end

Summary

Callbacks

Atom identifier for this block.

Renders the block to an HTML string given assigns.

Callbacks

name()

@callback name() :: atom()

Atom identifier for this block.

render(assigns)

@callback render(assigns :: map()) :: String.t()

Renders the block to an HTML string given assigns.