PhoenixPageMeta (PhoenixPageMeta v0.2.0)

Copy Markdown View Source

Per-page metadata for Phoenix LiveView apps: breadcrumbs, active-link state, SEO meta tag rendering.

Each page is described by a PhoenixPageMeta.PageMeta struct, which a LiveView returns from its page_meta/2 callback. The library renders the SEO meta tags, builds the breadcrumb trail, and resolves active-link state from it.

Setup

See PhoenixPageMeta.PageMeta for the struct and its configuration. In short:

# config/config.exs (all optional)
config :phoenix_page_meta,
  site_name: "MyApp",
  supported_locales: [:en, :es],
  extra_fields: [icon: nil]

# in a LiveView
%PhoenixPageMeta.PageMeta{title: "Hello", path: "/hello"}

In LiveViews

In MyAppWeb.live_view/0:

def live_view do
  quote do
    use Phoenix.LiveView, layout: ...
    @behaviour PhoenixPageMeta.LiveView
    import PhoenixPageMeta.LiveView, only: [assign_page_meta: 1]
  end
end

In each LiveView:

defmodule MyAppWeb.SomeLive do
  use MyAppWeb, :live_view

  @impl PhoenixPageMeta.LiveView
  def page_meta(_socket, _action) do
    %PhoenixPageMeta.PageMeta{title: "Hello", path: "/hello"}
  end

  def mount(_params, _session, socket) do
    {:ok, assign_page_meta(socket)}
  end
end

See PhoenixPageMeta.PageMeta, PhoenixPageMeta.Breadcrumb, PhoenixPageMeta.Components.Breadcrumbs, PhoenixPageMeta.Components.MetaTags, PhoenixPageMeta.Site, and PhoenixPageMeta.LiveView.

Deprecated: use PhoenixPageMeta

Declaring a per-app PageMeta module with use PhoenixPageMeta + a hand-rolled defstruct is deprecated as of 0.2.0 and will be removed in a future release. Use the PhoenixPageMeta.PageMeta struct instead.

Summary

Functions

__using__(opts) deprecated

Injects the PhoenixPageMeta wiring into a project PageMeta module.

Returns true if the given link path matches the current page or one of its ancestor paths.

Functions

__using__(opts)

(macro)
This macro is deprecated. Use the prebuilt `PhoenixPageMeta.PageMeta` struct instead — see CHANGELOG 0.2.0.

Injects the PhoenixPageMeta wiring into a project PageMeta module.

Adds @behaviour PhoenixPageMeta.Site, default implementations of base_url/0 and lang_path/2 (both defoverridable), wrappers for breadcrumbs/1 and active?/2,3 that match the project struct, and an @after_compile hook that validates the struct has the required fields.

Deprecated

use PhoenixPageMeta together with a hand-rolled defstruct is deprecated as of 0.2.0. Prefer the prebuilt, config-driven PhoenixPageMeta.PageMeta struct, which needs no per-app boilerplate. This macro will be removed in a future release.

active?(page_meta, link_path, opts \\ [])

Returns true if the given link path matches the current page or one of its ancestor paths.

Options

  • :exact — when true, only matches the current page exactly. Default false.
  • :query — when true, query strings are part of the comparison. When false (default), they are stripped from both sides before matching.

Matching rules

Without :exact, a link is active if its path equals the current path or is a prefix followed by /. So /locations matches /locations and /locations/123, but not /location-foo.

Most projects call this via the project module's wrapper (e.g. MyAppWeb.PageMeta.active?/2,3), which restricts the input type via %MyAppWeb.PageMeta{} pattern. The lib-level function accepts any struct.