Phlex Documentation

repository·main·Indexed 23 days ago

https://github.com/yippee-fun/phlex

Phlex 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.

Tokens
2.6K
Snippets
6
Records
35
Agent score
81%

What's inside Phlex

  1. Implement a Phlex::CSV component

    main

    To generate CSV output, subclass Phlex::CSV and provide a collection to the initializer. You must implement the row_template method to define how each row is constructed using the column method.

    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 the call method.

    Legacy Support: If your component uses the old view_template method name, Phlex::CSV will automatically alias it to row_template and issue a deprecation warning.

  2. Configure header rendering in Phlex::CSV

    main

    By default, Phlex::CSV renders a header row based on the first argument passed to the column method in your first row. You can disable this behavior by overriding render_headers? in your subclass.

    • true (default): Renders the header row.
    • false: Skips the header row.
    def render_headers?
      false
    end
  3. Configure whitespace trimming in Phlex::CSV

    main

    You can control whether leading and trailing whitespace is stripped from CSV values by overriding the trim_whitespace? method in your Phlex::CSV subclass.

    • true: Strips leading and trailing whitespace from all values.
    • false (default): Preserves whitespace as provided.
    def trim_whitespace?
      true
    end
  4. Configure CSV injection escaping in Phlex::CSV

    main

    CSV injection occurs when malicious formulae (starting with =, +, -, @, \t, or \r) are used to execute code in spreadsheet software. You must override escape_csv_injection? in your Phlex::CSV subclass 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::CSV will raise an error to prevent accidental vulnerability.

  5. Customize the CSV delimiter

    main

    The call method accepts a delimiter option 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::ArgumentError is raised.

  6. Generate SVG strings using Phlex.svg

    main
    Use Phlex.svg to generate an SVG string using Phlex's SVG DSL. Similar to Phlex.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.