Phlex.SGML behaviour (phlex v0.3.0)

Copy Markdown View Source

Standard Generalized Markup Language foundation for Phlex.

This module provides the base functionality shared between HTML and SVG components, including state management, buffer handling, and attribute generation.

Example

defmodule MyComponent do
  use Phlex.SGML

  def render_template(assigns, state) do
    state
    |> append_text("Hello, World!")
  end
end

MyComponent.render()
# => "Hello, World!"

Summary

Callbacks

The render template that must be implemented by components.

Functions

Generates attributes string from a keyword list or map and appends to state buffer.

Appends a SafeObject without escaping. Matches Phlex Ruby raw.

Appends text content to the buffer, escaping HTML entities.

Caches a block of content based on a cache key.

Captures the output of a block without rendering it.

Wraps the output in an HTML comment.

Returns the user context map from a state.

Defines a fragment that can be selectively rendered.

Generates attributes string from a keyword list or map.

Caches a block with an explicit cache key and store.

Output plain text (HTML-escaped). Matches Phlex Ruby plain.

Polymorphic nested render aligned with Phlex Ruby render.

Renders another component into the current state.

Returns true if the component is currently rendering, false otherwise.

Marks a string as safe for HTML output.

Appends raw HTML without a SafeObject wrapper.

Outputs whitespace. If a block is given, outputs whitespace before and after the block.

Callbacks

view_template(component, state)

@callback view_template(component :: struct(), state :: Phlex.SGML.State.t()) ::
  Phlex.SGML.State.t()

The render template that must be implemented by components.

Functions

append_attributes(state, attributes)

Generates attributes string from a keyword list or map and appends to state buffer.

append_raw(state, content)

Appends a SafeObject without escaping. Matches Phlex Ruby raw.

Pass binaries through safe/1, or use unsafe_raw/2 for an explicit opt-in.

append_text(state, content)

Appends text content to the buffer, escaping HTML entities.

cache(state, fun)

Caches a block of content based on a cache key.

Uses a process-local FIFO cache store by default. Keys include the calling module context when available via state.user_context[:phlex_cache_class].

Calling cache/2 with only a function uses an empty key fragment, matching Ruby cache { ... }.

cache(state, cache_key, fun)

capture(state, fun)

Captures the output of a block without rendering it.

Useful for extracting content for caching or other processing.

comment(state, fun)

Wraps the output in an HTML comment.

Example

comment(state, fn state ->
  Phlex.SGML.append_text(state, "This is a comment")
end)

context(state)

Returns the user context map from a state.

This is a module-level function that can be called directly.

fragment(state, fragment_id, fun)

Defines a fragment that can be selectively rendered.

Fragments allow you to render only specific parts of a component, which is useful for partial page updates and caching.

Example

def view_template(_assigns, state) do
  state
  |> div([], fn state ->
    state
    |> h1([], fn state ->
      Phlex.SGML.append_text(state, "Header")
    end)
    |> fragment("content", fn state ->
      state
      |> p([], fn state ->
        Phlex.SGML.append_text(state, "This is a fragment")
      end)
    end)
  end)
end

Then render only the fragment:

MyComponent.render(fragments: MapSet.new(["content"]))

generate_attributes(attributes)

Generates attributes string from a keyword list or map.

low_level_cache(state, cache_key, fun)

Caches a block with an explicit cache key and store.

cache_store may be a Phlex.FIFOCacheStore or any module/value accepted by fetch_from_cache_store/3. When omitted-style atoms are passed, the process default store is used.

low_level_cache(state, cache_key, cache_store, fun)

plain(state, content)

Output plain text (HTML-escaped). Matches Phlex Ruby plain.

raw(state, content)

render(state, renderable)

Polymorphic nested render aligned with Phlex Ruby render.

Accepts component modules or structs, binaries (via plain/2), lists of renderables, arity-1 functions, or nil with an optional :content_block.

render(state, component, opts)

render_component(state, component_module, assigns \\ %{})

Renders another component into the current state.

Prefer render/2 or render/3 for the full polymorphic Phlex Ruby surface.

rendering?(state)

Returns true if the component is currently rendering, false otherwise.

safe(content)

Marks a string as safe for HTML output.

Example

safe_html = Phlex.SGML.safe("<strong>Hello</strong>")
Phlex.SGML.append_raw(state, safe_html)

unsafe_raw(state, content)

Appends raw HTML without a SafeObject wrapper.

Prefer raw/2 with safe/1 for trusted markup.

whitespace(state)

Outputs whitespace. If a block is given, outputs whitespace before and after the block.

Example

whitespace(state)
whitespace(state, fn state ->
  Phlex.SGML.append_text(state, "content")
end)

whitespace(state, fun)