1. 15

Joel Drapper’s new ruby view library released version 1.0 today.

  1. 4

    I used this in a PR for phlex.fun (yes, their site uses it) and to play with in a private hacky script for creating Confluence pages. It’s really fun to use! I plan on using it for future projects, 100%.

    Here’s the thing: non-Rubyists will not like it, and might not even be able to work effectively with it. You’ve GOT to be invested in Ruby for it to be considered. That’s going to mean it won’t fly for a lot of teams or organizations. That’s OK by me.

    1. 4

      I’m using this on the job and it makes me so happy. The gem https://github.com/digital-fabric/papercraft had a similar style that I was very excited about, but was left unfinished and was over my head technically. Phlex’s implementation is way easier to follow and Joel is v responsive to feedback and PRs.

      Strong recommend.

      1. 3

        Reminds me a lot haml, but that’s an external language. I guess it comes closer to _why’s old markaby DSL.

        1. 2

          I don’t have a problem with other people liking it and using it themselves, but this just reminds me of Haml. They’re trying to solve a problem that I don’t have – regular HTML seems just fine to me.

          1. 4

            For a static page, HTML is good.

            For a dynamic page in a Ruby application, you end up with Ruby + HTML + ERB + (in rails) partials with fuzzy unclear interfaces.

            With Phlex you end up with Ruby + HTML, and objects have clear interfaces. Also performance is significantly better, and you can do some really neat things (example) with object composition. And Ruby is fun to write (for rubyists).

            It’s not for everyone, but it’s a refreshing approach for ruby apps (not just rails!) that I really dig as a rubyist.

            1. 4

              Plain HTML is not comparable to Phlex because plain HTML does not support dynamic values or embedding templates into other templates.

              By “regular HTML”, do you mean what Ruby on Rails supports by default? That would be HTML embedded in a variant of ERB using Action View.

              For comparison, here’s an (untested) example of HTML and ERB using Action View that demonstrates passing a score value down to a sub-template:

              in a Rails controller action test_results
              @score = calculate_score
              # the view is implicitly called
              
              test_results.html.erb
              <p>You scored <%= @score %> points.</p>
              <%= render partial: "judgement", locals: {score: @score} %>
              
              _judgement.html.erb
              <div class="judgement">
                <% if score > 5 %>
                  Great job!
                <% else %>
                  You could do better.
                <% end %>
              </div>
              

              Based on Phlex’s documentation, I think it would let you write that Rails view like this:

              in the Rails controller action
              render TestResults.new(
                score: calculate_score
              )
              
              test_results.rb
              class TestResults < Phlex::HTML
                def initialize(score:)
                  @score = score
                end
              
                def template
                  p { "You scored #{@score} points." }
                  judgement
                end
              
                def judgement
                  div(class: 'judgement') do
                    if @score > 5
                      'Great job!'
                    else
                      'You could do better.'
                    end
                  end
                end
              end