phlex.fun icon indicating copy to clipboard operation
phlex.fun copied to clipboard

Docs never mention what to call to render the view

Open zavan opened this issue 3 years ago • 3 comments

I noticed that although the docs have a lot of examples of how to define the view classes, they don't mention how to render them (without Rails) at all (that I could find, maybe I missed it). I mean, what method do I call to get the HTML string?

I actually had to go and check the source code to find out you can call .call / .() on an instance or call it on the class directly with the args (also aliased as render on the class):

class Nav < Phlex::HTML
  def initialize(items:)
    @items = items
  end

  def template
    nav {
      ul {
        @items.each do |item|
          li { a(href: item[:url]) { item[:label] } }
        end
      }
    }
  end
end

items = [
  { url: 'https://www.phlex.fun/', label: 'Docs' },
  { url: 'https://github.com/joeldrapper/phlex', label: 'Home' }
]

Nav.new(items:).call

# or

Nav.call(items:)

# Or

Nav.render(items:)

# => <nav><ul><li><a href="https://www.phlex.fun/">Docs</a></li><li><a href="https://github.com/joeldrapper/phlex">Home</a></li></ul></nav>

zavan avatar Feb 24 '23 17:02 zavan

Thanks for opening this. Definitely should document that. You never need to think about this when using it with rails but definitely need it without rails.

joeldrapper avatar Feb 24 '23 18:02 joeldrapper

I also faced this issue. I started a bare-bones experimental rack project just to use Phlex and I didn't know how to render the views. I even thought (after a few failed attempts) that I had to use it with a framework and it wasn't supposed to be used by itself. Then I checked the source code and I realized that call() was all I needed.

Came here to create an issue.

aesmail avatar Apr 21 '23 10:04 aesmail

Thanks @aesmail. I’m putting together a new documentation site at https://github.com/joeldrapper/phlex.fun but it's taking a while. There's also full API docs available here https://rubydoc.info/gems/phlex

joeldrapper avatar Apr 21 '23 10:04 joeldrapper