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
@callback view_template(component :: struct(), state :: Phlex.SGML.State.t()) :: Phlex.SGML.State.t()
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.
Pass binaries through safe/1, or use unsafe_raw/2 for an explicit opt-in.
Appends text content to the buffer, escaping HTML entities.
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 { ... }.
Captures the output of a block without rendering it.
Useful for extracting content for caching or other processing.
Wraps the output in an HTML comment.
Example
comment(state, fn state ->
Phlex.SGML.append_text(state, "This is a comment")
end)
Returns the user context map from a state.
This is a module-level function that can be called directly.
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)
endThen render only the fragment:
MyComponent.render(fragments: MapSet.new(["content"]))
Generates attributes string from a keyword list or map.
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.
Output plain text (HTML-escaped). Matches Phlex Ruby plain.
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.
Renders another component into the current state.
Prefer render/2 or render/3 for the full polymorphic Phlex Ruby surface.
Returns true if the component is currently rendering, false otherwise.
Marks a string as safe for HTML output.
Example
safe_html = Phlex.SGML.safe("<strong>Hello</strong>")
Phlex.SGML.append_raw(state, safe_html)
Appends raw HTML without a SafeObject wrapper.
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)