AshReports.Renderer behaviour (ash_reports v0.1.0)
Enhanced behaviour for report renderers with context management and layout support.
This module defines the core renderer behaviour for the Phase 3.1 Renderer Interface, providing a comprehensive API for format-specific rendering with advanced features including context management, layout calculation, and streaming support.
Each output format (HTML, PDF, HEEX, JSON) implements this behaviour to provide format-specific rendering capabilities while benefiting from the unified context and layout management system.
Enhanced Features (Phase 3.1)
- Context-Aware Rendering: Renderers receive RenderContext with complete state
- Layout Integration: Automatic layout calculation and element positioning
- Streaming Support: Built-in support for large dataset streaming
- Error Recovery: Comprehensive error handling and recovery mechanisms
- Performance Monitoring: Built-in performance tracking and optimization
Renderer Implementation
Format-specific renderers must implement the enhanced callback interface:
defmodule MyReport.Html do
@behaviour AshReports.Renderer
def render_with_context(context, opts) do
# Render using context
end
def supports_streaming?(), do: true
def file_extension(), do: "html"
def content_type(), do: "text/html"
endIntegration with Phase 2
Renderers seamlessly integrate with Phase 2 DataLoader results through RenderContext:
{:ok, data_result} = DataLoader.load_report(domain, :sales_report, params)
context = RenderContext.new(report, data_result, config)
{:ok, output} = Renderer.render_with_context(renderer, context)
Summary
Callbacks
Cleans up after rendering operations.
The MIME content type for this format.
The file extension for this format.
Prepares the renderer for rendering operations.
Legacy render callback for backward compatibility.
Enhanced render callback with context support.
Whether this renderer supports streaming output.
Validates that the renderer can handle the given context.
Functions
Creates a RenderContext from a report and DataLoader result.
Gets available renderers for a report module.
Gets renderer information for a specific format.
Renders a report using the appropriate renderer for the given format.
Renders a report directly from DataLoader results.
Renders a report using the enhanced context-aware API.
Checks if a renderer supports the given format.
Validates a renderer module implements the required callbacks.
Types
Callbacks
@callback cleanup(AshReports.RenderContext.t(), render_result()) :: :ok
Cleans up after rendering operations.
Optional callback for cleanup.
@callback content_type() :: String.t()
The MIME content type for this format.
@callback file_extension() :: String.t()
The file extension for this format.
@callback prepare(AshReports.RenderContext.t(), opts()) :: {:ok, AshReports.RenderContext.t()} | {:error, term()}
Prepares the renderer for rendering operations.
Optional callback for initialization.
@callback render(report_module(), data(), opts()) :: {:ok, rendered()} | {:error, term()}
Legacy render callback for backward compatibility.
Prefer implementing render_with_context/2 for new renderers.
@callback render_with_context(AshReports.RenderContext.t(), opts()) :: {:ok, render_result()} | {:error, term()}
Enhanced render callback with context support.
This is the preferred implementation for Phase 3.1 renderers.
@callback supports_streaming?() :: boolean()
Whether this renderer supports streaming output.
@callback validate_context(AshReports.RenderContext.t()) :: :ok | {:error, term()}
Validates that the renderer can handle the given context.
Optional callback for advanced validation.
Functions
@spec create_context(any(), map(), map()) :: AshReports.RenderContext.t()
Creates a RenderContext from a report and DataLoader result.
Convenience function for creating context from Phase 2 output.
Examples
{:ok, data_result} = DataLoader.load_report(domain, :report_name, params)
context = Renderer.create_context(report, data_result, config)
Gets available renderers for a report module.
Examples
renderers = Renderer.get_available_renderers(MyReport)
Gets renderer information for a specific format.
Examples
info = Renderer.get_renderer_info(MyReport, :html)
Renders a report using the appropriate renderer for the given format.
Legacy API for backward compatibility. Prefer render_with_context/3.
@spec render_from_data_result(module(), any(), map(), map(), opts()) :: {:ok, render_result()} | {:error, term()}
Renders a report directly from DataLoader results.
High-level API that combines context creation and rendering.
Examples
{:ok, data_result} = DataLoader.load_report(domain, :sales_report, params)
{:ok, output} = Renderer.render_from_data_result(
renderer,
report,
data_result,
config
)
@spec render_with_context(module(), AshReports.RenderContext.t(), opts()) :: {:ok, render_result()} | {:error, term()}
Renders a report using the enhanced context-aware API.
This is the main entry point for Phase 3.1 rendering with full context and layout support.
Examples
context = RenderContext.new(report, data_result, config)
{:ok, result} = Renderer.render_with_context(renderer, context)
Checks if a renderer supports the given format.
Examples
if Renderer.supports_format?(MyReport, :pdf) do
render_as_pdf()
end
Validates a renderer module implements the required callbacks.
Examples
case Renderer.validate_renderer_module(MyRenderer) do
:ok -> use_renderer(MyRenderer)
{:error, missing} -> handle_missing_callbacks(missing)
end