Phlex Documentation
repository·main·Indexed 23 days ago
https://github.com/yippee-fun/phlexPhlex is a framework for building object-oriented web views in pure Ruby, moving UI logic from traditional templates into Ruby objects. It provides DSLs for generating HTML and SVG strings, a compilation system via Phlex::Compiler, and specialized components like Phlex::CSV for CSV generation and Phlex::FIFOCacheStore for high-performance in-memory caching. The library follows the BreakVer versioning specification rather than SemVer.
What's inside Phlex
- Phlex is a library that allows you to build object-oriented web views using pure Ruby. It provides a way to define UI components as Ruby objects rather than using template files.
Understand Phlex versioning (BreakVer)
mainPhlex does not use Semantic Versioning (SemVer). Instead, it follows the BreakVer specification. When upgrading, be aware that version numbers do not follow the standard major.minor.patch logic used by SemVer.Define a component template with `view_template`
mainIn Phlex 2.x, components define their rendering logic within aview_templatemethod. If you are upgrading from Phlex 1.x, you must rename yourtemplatemethod toview_template.Implement a Phlex::CSV component
mainTo generate CSV output, subclass
Phlex::CSVand provide a collection to the initializer. You must implement therow_templatemethod to define how each row is constructed using thecolumnmethod.Important Security Requirement: You must define the
escape_csv_injection?method in your class to specify how to handle CSV injection vulnerabilities. If left undefined, the component will raise an error during thecallmethod.Legacy Support: If your component uses the old
view_templatemethod name,Phlex::CSVwill automatically alias it torow_templateand issue a deprecation warning.Configure header rendering in Phlex::CSV
mainBy default,
Phlex::CSVrenders a header row based on the first argument passed to thecolumnmethod in your first row. You can disable this behavior by overridingrender_headers?in your subclass.true(default): Renders the header row.false: Skips the header row.
def render_headers? false endConfigure whitespace trimming in Phlex::CSV
mainYou can control whether leading and trailing whitespace is stripped from CSV values by overriding the
trim_whitespace?method in yourPhlex::CSVsubclass.true: Strips leading and trailing whitespace from all values.false(default): Preserves whitespace as provided.
def trim_whitespace? true endConfigure CSV injection escaping in Phlex::CSV
mainCSV injection occurs when malicious formulae (starting with
=,+,-,@,\t, or\r) are used to execute code in spreadsheet software. You must overrideescape_csv_injection?in yourPhlex::CSVsubclass to choose a security posture:true: Prefixes values starting with formula characters with a single quote (') to neutralize them. This may impact data integrity.false: Disables all escaping. Use this only if the CSV is intended for byte-for-byte data exchange between secure systems and will not be opened in spreadsheet programs like Excel or Google Sheets.
If you do not define this method,
Phlex::CSVwill raise an error to prevent accidental vulnerability.Get the HTML content type
mainThecontent_typemethod returns the string `Customize the CSV delimiter
mainThe
callmethod accepts adelimiteroption to change the character used to separate values. The delimiter must be a single character.If an invalid delimiter (length != 1) is provided, a
Phlex::ArgumentErroris raised.Generate SVG strings using Phlex.svg
mainUsePhlex.svgto generate an SVG string using Phlex's SVG DSL. Similar toPhlex.html, the block's receiver's instance variables are transferred to the Phlex component context, enabling the use of local state within the SVG definition.Clear the Phlex::FIFOCacheStore
mainYou can completely empty the cache by calling theclearmethod. This removes all keys and values currently stored in the FIFO queue.Render Phlex components to a String
mainYou can render a component instance or its class directly to a String using the.callmethod. When calling.callon a class, it instantiates the component and then executes the render logic.