phlex icon indicating copy to clipboard operation
phlex copied to clipboard

Compiled components

Open joeldrapper opened this issue 1 year ago • 0 comments

We should be able to compile components into methods.

class NavComponent < Phlex::Component
  def initialize(links:)
    super
  end
  
  # Given this template
  def template
    nav do
      ul do
        @links.each do |label, url|
          li { a label, href: url }
        end
      end
    end
  end

  # The generated compiled template would look something like this
  def compiled_template
    @_buffer << "<nav><ul>"
    @links.each do |label, url|
      @_buffer << "<li><a href=\"#{url}\">#{label}</a></li>"
    end
    @_buffer << "</ul></nav>"
  end
end

joeldrapper avatar Jul 08 '22 16:07 joeldrapper