Phlex::SGML
The base class for HTML and SVG components in Phlex. Provides core rendering functionality, content output methods, lifecycle hooks, and caching capabilities.Class Methods
.call(*args, **kwargs, &block)
Render the view to a String. Arguments are delegated to .new.
Positional arguments passed to the component’s initializer
Keyword arguments passed to the component’s initializer
Content block sent to
view_template during rendering (not passed to initialize).new(*args, **kwargs, &block)
Create a new instance of the component.
The block is NOT delegated to
#initialize. Instead, it will be sent to #view_template when rendering.Instance Methods
#view_template(&block)
Define the template for your component. Override this method in your component class.
Optional content block passed during rendering
#call(buffer = +"", context: {}, fragments: nil, &block)
Render the component and return the output string.
Output buffer to append to
Hash of context data available during rendering via
#contextArray of fragment names to selectively render
Content block to render
#context
Access the context hash passed to #call.
#rendering?
Returns false before rendering and true once the component has started rendering. Does not reset to false after rendering.
#plain(content)
Output plain text content with automatic HTML escaping.
Content to output as plain text
Supports String, Symbol, Integer, and Float types. Other types require custom
format_object implementation.#whitespace(&block)
Output a single space character. If a block is given, a space will be output before and after the block.
#comment(&block)
Wrap the output in an HTML comment.
#raw(content)
Output the given safe object as-is without escaping. Use #safe to mark a string as safe.
Safe object to output without escaping
#safe(value)
Mark a string as safe for HTML output, preventing automatic escaping.
String to mark as safe
🦺
#capture(*args, &block)
Capture the output of a block and return it as a string instead of appending to the buffer.
Arguments to pass to the block
Block to capture output from
#fragment(name, &block)
Define a named fragment that can be selectively rendered.
Name of the fragment
#flush
Flush the current state buffer to the output buffer.
#render(renderable = nil, &block)
Render another component, enumerable, string, or proc.
Object to render
#cache(*cache_key, **options, &content)
Cache a block of content with an automatically generated cache key.
Custom cache key components (combined with automatic keys)
Options passed to the cache store
The full cache key includes: app version, class name, object_id (if reloading enabled), method name, line number, and your custom cache key.
#low_level_cache(cache_key, **options, &content)
Cache a block where you control the entire cache key.
Complete cache key (you have full control)
Options passed to the cache store
#json_escape(string)
Escape a string for safe use in JSON contexts.
String to escape for JSON
#to_proc
Convert the component to a Proc that can be used with collection methods.
Lifecycle Hooks
Override these private methods to hook into the rendering lifecycle:#around_template(&block)
Wrap the entire template rendering. Call yield to render the template.
#before_template(&block)
Called before rendering the template.
#after_template(&block)
Called after rendering the template.
#render?
Control whether the component should render. Return false to skip rendering.
Customization Hooks
#format_object(object)
Customize how objects are formatted for output. Override to support additional types.
#cache_store
Provide a cache store implementation for #cache and #low_level_cache.
#app_version_key
Override to use a different deployment key for cache invalidation.
#enable_cache_reloading?
Enable cache reloading during development. Returns false by default.