phlex-ruby / phlex.fun

Website and documentation for Phlex
https://beta.phlex.fun
MIT License
33 stars 26 forks source link

Docs never mention what to call to render the view #16

Closed zavan closed 4 weeks ago

zavan commented 1 year ago

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>
joeldrapper commented 1 year ago

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.

aesmail commented 1 year ago

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.

joeldrapper commented 1 year ago

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