Sayfa.Template (Sayfa v0.5.0)

Copy Markdown View Source

EEx template rendering with three-layer composition.

Templates are rendered in three layers:

  1. Content body — Markdown already rendered to HTML (from Sayfa.Content)
  2. Layout template — Wraps content body (e.g., article.html.eex, page.html.eex)
  3. Base template — HTML shell (<html>, <head>, <body>), wraps layout output

Layout Selection

The layout is determined by (in priority order):

  1. Front matter layout: key
  2. Content type directory default (articles/article, pages/page)
  3. Fallback to page

Examples

# Render an EEx string
{:ok, html} = Sayfa.Template.render_string("<h1><%= @title %></h1>", title: "Hello")

# Full three-layer render
{:ok, html} = Sayfa.Template.render_content(content, config: config)

Summary

Functions

Renders content through the full three-layer template pipeline.

Renders an error page (e.g. 404) through the base template.

Renders an EEx template file with the given assigns.

Renders a list page through the three-layer template pipeline.

Renders an EEx string with the given assigns.

Functions

render_content(content, opts)

@spec render_content(
  Sayfa.Content.t(),
  keyword()
) :: {:ok, String.t()} | {:error, term()}

Renders content through the full three-layer template pipeline.

  1. Determines the layout from content metadata or content type
  2. Renders the layout template with content body as @inner_content
  3. Wraps the result in the base template

Options

  • :config — resolved config map (required)
  • :layouts_dir — override layouts directory (optional, derived from config)

Examples

config = Sayfa.Config.resolve([])
{:ok, html} = Sayfa.Template.render_content(content, config: config)

render_error_page(layout_name, opts)

@spec render_error_page(
  String.t(),
  keyword()
) :: {:ok, String.t()} | {:error, term()}

Renders an error page (e.g. 404) through the base template.

Uses the given layout name (e.g. "404") and wraps it in the base template with minimal assigns.

Examples

{:ok, html} = Sayfa.Template.render_error_page("404", config: config)

render_file(template_path, assigns)

@spec render_file(
  String.t(),
  keyword()
) :: {:ok, String.t()} | {:error, term()}

Renders an EEx template file with the given assigns.

Examples

Sayfa.Template.render_file("priv/default_theme/layouts/page.html.eex",
  inner_content: "<p>Hello</p>",
  content: content,
  site: config
)

render_list_page(opts)

@spec render_list_page(keyword()) :: {:ok, String.t()} | {:error, term()}

Renders a list page through the three-layer template pipeline.

Used for archive pages (tags, categories) and content type index pages. Uses the list.html.eex layout with @contents and @pagination assigns.

Options

  • :config — resolved config map (required)
  • :layouts_dir — override layouts directory (optional)
  • :contents — list of content items to display (required)
  • :page_title — title for the page (required)
  • :pagination — pagination struct (optional, nil for non-paginated)

render_string(eex_string, assigns)

@spec render_string(
  String.t(),
  keyword()
) :: {:ok, String.t()} | {:error, term()}

Renders an EEx string with the given assigns.

Examples

iex> Sayfa.Template.render_string("Hello <%= @name %>", name: "World")
{:ok, "Hello World"}