PhoenixKitOG.Variables (PhoenixKitOG v0.1.1)

Copy Markdown View Source

Registry + resolver for module-exposed variables that templates can bind their slots to.

Sources

  • Global — always available regardless of consumer module. Owned by this module (see global/0). Includes site host, site URL, site name, current page URL, current locale.

  • Module-declared — external PhoenixKit modules opt in by implementing two optional callbacks:

    def og_variables, do: [
      %{name: "post_title", type: :text, label: "Post title", description: "..."},
      %{name: "post_featured_image", type: :image, label: "Featured image"}
    ]
    
    def og_resolve(var_name, context)
    # context = %{module_key: "publishing", resource: post_map, conn: conn, language: "en"}

    og_variables/0 declares the shape; og_resolve/2 fetches the value at render time. Both are auto-discovered via PhoenixKit.ModuleDiscovery.

Globals + module-declared variables are combined at assignment-time in the "wire slots" dropdown — the user sees one list per slot, scoped by matching type.

Summary

Functions

Merged list — globals + everything a consumer module declares.

Flat map of all [[global]] values, ready to merge into the substitution map. Prefers conn fields (real request context); if no conn is available, falls back to the Phoenix endpoint module (passed as :endpoint) so the editor can preview real values without a request.

Given the slots a template uses, an assignment's slot_mapping (%{slot_name => variable_name}), and a resolution context, produces the substitution values map for the renderer.

Types

variable()

@type variable() :: %{
  name: String.t(),
  type: :text | :image,
  label: String.t(),
  description: String.t()
}

Functions

for_module(module_key)

@spec for_module(String.t()) :: [variable()]

Merged list — globals + everything a consumer module declares.

global()

@spec global() :: [variable()]

global_values(context \\ %{})

@spec global_values(map()) :: %{required(String.t()) => String.t()}

Flat map of all [[global]] values, ready to merge into the substitution map. Prefers conn fields (real request context); if no conn is available, falls back to the Phoenix endpoint module (passed as :endpoint) so the editor can preview real values without a request.

resolve(slots, slot_mapping, context)

@spec resolve([PhoenixKitOG.Slots.t()], %{required(String.t()) => String.t()}, map()) ::
  %{
    required(String.t()) => String.t()
  }

Given the slots a template uses, an assignment's slot_mapping (%{slot_name => variable_name}), and a resolution context, produces the substitution values map for the renderer.

Missing wires (slot not in slot_mapping) or unknown variable names return nil for that slot — the renderer leaves {{slot}} visible, matching the workspace convention.