Phlex Rails
repository·main·Indexed 18 days ago
https://github.com/yippee-fun/phlex-railsPhlex Rails integrates the Phlex view engine into Rails, allowing developers to build web views using pure Ruby instead of traditional template engines like ERB or Haml. It provides specialized classes for HTML and SVG rendering, access to Rails view helpers via HelperMacros, and support for Rails layouts, translations, and partials within Phlex components.
What's inside phlex-rails
- Phlex Rails is a framework that enables you to compose web views using pure Ruby code, integrating the Phlex view engine into a Rails application environment.
Access Phlex Documentation
mainComprehensive documentation for Phlex and its integration can be found at www.phlex.fun.Get Support for Phlex Rails
mainIf you encounter issues while using Phlex Rails:
- For general questions or trouble, start a discussion.
- To report a bug, open an issue.
How Phlex::Rails::Layout works with Rails view contexts
mainThe
Phlex::Rails::Layoutmodule provides an interface for rendering Phlex components as Rails layouts. When included in a class inheriting fromPhlex::HTML, it enables the component to interact with the Railsview_context.Key behaviors:
- Rendering: The
rendermethod handles the transition between Phlex and the Rails view flow. If a block yields aSymbol, the layout attempts to resolve that symbol usingview_context.view_flow.get(symbol). Otherwise, it yields the block normally. - Virtual Paths: The module automatically generates a
virtual_pathbased on the class name. It converts Ruby constant names (e.g.,Admin::UserLayout) into Rails-style underscored paths (e.g.,admin.user_layout). This is useful for mapping Phlex layouts to Rails view lookup conventions. - Helpers: It includes a suite of Rails-specific tag helpers such as
CSPMetaTag,CSRFMetaTags,StylesheetLinkTag, andTurboRefreshtags, allowing you to use standard Rails asset and meta tags directly within your Phlex layout classes.
class ApplicationLayout < Phlex::HTML include Phlex::Rails::Layout def template head do # Helpers included via Phlex::Rails::Layout csrf_meta_tags javascript_importmap_tags end body do yield end end end- Rendering: The
How component-scoped translation paths are resolved
mainWhen you use a scoped translation key (starting with
.), the helper calculates atranslation_pathbased on the component's class name.::is replaced with.- CamelCase is converted to snake_case (e.g.,
MyComponentbecomesmy_component). - The resulting path is prepended to your key.
Example: A component named
Admin::UserDashboardwill resolve.titletoadmin.user_dashboard.title.How Phlex::Rails::Buffered handles component output
mainIn
phlex-rails,Phlex::Rails::Bufferedis a wrapper object used to manage how objects are rendered within a Phlex component. It acts as a proxy to an underlying@object, intercepting method calls to ensure that the output is correctly captured and passed through the component's rendering pipeline.When a method is called on a
Bufferedobject:- If a block is provided: The method is called on the underlying object, and the block is passed to
@component.capture. The result is then wrapped in@component.rawto ensure it is treated as safe HTML/output. - If no block is provided: The method is called on the underlying object, and the result is passed through
@component.raw.
This mechanism allows standard Ruby objects to behave as if they are part of the Phlex rendering flow, automatically handling content capture and raw output injection.
# Conceptual usage within a component context # The Buffered object intercepts calls to ensure they are captured by the component # and marked as raw output. # If @object is a helper or another component: # @component.raw(@object.method_name) if no block # @component.raw(@object.method_name { |args| @component.capture(args) }) if block- If a block is provided: The method is called on the underlying object, and the block is passed to
How Phlex::Rails::Builder handles method delegation
mainThe
Phlex::Rails::Builderis a proxy object used during the construction of Phlex components within a Rails environment. It wraps a target object (typically a Rails view context or a builder object) and intercepts method calls to facilitate seamless integration between Phlex and Rails-specific output types.When a method is called on the builder:
- With a block: The builder executes the method on the underlying object and yields a new
Phlex::Rails::Builderinstance to the block. This allows for nested Phlex component construction. - Without a block: The builder simply delegates the method call to the underlying object.
- Output Handling: If the result of the method call is an
ActiveSupport::SafeBuffer(common in Rails for HTML-safe strings), the builder automatically wraps the output using@component.raw(output)to ensure correct rendering within the Phlex component.
# Conceptual usage pattern within a Phlex component # The builder ensures Rails-specific helpers return correctly to Phlex def construct # Inside a Phlex component, the builder might be used to wrap # Rails view helpers that return SafeBuffer strings. content.tag.div do # If 'link_to' returns a SafeBuffer, the Builder wraps it via @component.raw link_to "Home", root_path end end- With a block: The builder executes the method on the underlying object and yields a new
Access Rails view helpers in Phlex components
mainPhlex Rails integrates Rails view helpers into the Phlex rendering lifecycle. By using
Phlex::HTMLorPhlex::SVG, your components gain access to a wide array of Rails helpers viaPhlex::Rails::HelperMacros.Commonly supported helpers include:
- Asset helpers:
asset_url,image_url,stylesheet_link_tag,javascript_include_tag. - Form/URL helpers:
url_for,url_options,url_to_asset. - Meta tags:
csrf_meta_tags,csp_meta_tag. - Content helpers:
textarea_tag,url_field_tag.
These helpers are automatically mapped to their corresponding Phlex classes via the Zeitwerk loader inflections.
- Asset helpers:
Install Phlex Rails via Rails generator
mainWhen adding
phlex-railsto a Rails project, you can run the installation generator to set up the necessary configuration files and base classes. This generator performs three main actions:- Creates a configuration initializer at
config/initializers/phlex.rb. - Creates a base component class at
app/components/base.rb. - Creates a base view class at
app/views/base.rb.
Run the following command in your terminal:
rails generate phlex:install- Creates a configuration initializer at
Report Security Vulnerabilities
mainIf you suspect a security vulnerability, do not open a public issue or pull request. Instead, please send a private advisory.Resolve missing Rails helper errors in Phlex components
mainIf you attempt to call a Rails view helper (e.g.,
link_to,image_tag) inside a Phlex component and it is not available,phlex-railswill raise aNoMethodErrorwith a specific suggestion.To fix this, you must include the appropriate helper module from
Phlex::Rails::Helpersin your component class. For example, if you are missing a helper that belongs to a specific module, the error message will tell you exactly which one to include.# Example of what the error suggests doing: class MyComponent < Phlex::SGML include Phlex::Rails::Helpers::SomeHelperModule def call # Now you can use helpers from SomeHelperModule end endGenerate Rails-compatible virtual paths from Phlex classes
mainWhen using
Phlex::Rails::Layout, you can determine thevirtual_pathof a layout class. This method transforms the Ruby class name into a lowercase, underscored string suitable for Rails view naming conventions.Transformation rules:
::is replaced with.- CamelCase is converted to snake_case (e.g.,
UserLayoutbecomesuser_layout) - The entire string is downcased.
# Example transformation class Admin::UserLayout < Phlex::HTML include Phlex::Rails::Layout end Admin::UserLayout.virtual_path # => "admin.user_layout"