trailblazer / formular

Form builder for Ruby. Fast, Furious, and Framework-Agnostic.
MIT License
81 stars 17 forks source link

Adding Haml support and teest #63

Open konung opened 6 years ago

konung commented 6 years ago

Hi Fran. Unless I'm missing something HAML support ( Haml 5.0.4 with corresponding cells-haml) is available out of the box ( I haven't tried hamlit). Just added a test & gemspec - based off of SLIM. You did all the hard work! 👍

konung commented 6 years ago

Ignore pull request #62 - I did git add . from wrong directory level 🤕

fran-worley commented 6 years ago

@konung Can you add an simple example in a branch in the demo repo too?

fran-worley commented 6 years ago

Thanks so much for adding this - I never quite got around to adding 'formal' support for it!

konung commented 6 years ago

Full stop. :( So I was doing this with my eyes half closed last night - because I didn't see that haml version produces the same form twices. ( or rather some elements of the form are output twice before the form). I've seen it before somewhere with simple_form I think, but I can't remember where or how I fixed. Anyways this is related to https://github.com/trailblazer/formular/issues/52

And will need to figure out a way around this first. I'll dive into formular later this week :)

fran-worley commented 6 years ago

@konung we have the same issue with erb actually and why we had to add a blockless api see the erb tests https://github.com/trailblazer/formular/blob/master/test/fixtures/comment/erb/show/view/show.erb

konung commented 6 years ago

Thank you Fran

This rabbit hole is insane. I figured out a temp solution and even the cause, but I'm not sure where to implement a permanent solution inside formular.

Problem So this has to do with how HAML ( and ERB ) uses output buffer. These issues pointed me in the right direction ( just organizing it here for posterity)

Reference Reading

  1. http://trailblazer.to/gems/cells/cells4.html#html-escaping
  2. https://stackoverflow.com/questions/24985539/nested-capture-haml-helpers
  3. https://stackoverflow.com/questions/12437597/cannot-cache-from-decorator-draper

And these HAMLIT (not haml) issues

  1. https://github.com/k0kubun/hamlit/issues/53
  2. https://github.com/hamlit/hamlit-block/issues/1
  3. https://github.com/k0kubun/hamlit/issues/56

Ad-Hoc Solution

So the solution that can be implement just in the HAML template is to capture contents of every block via _capturehaml

%div New
= form(model, "/posts", path_prefix: :comment) do |f|
  - capture_haml do
    = f.input :id
    = f.textarea :body
    = f.checkbox :public, value: true
    = f.collection :replies do |reply|
      - capture_haml do
        = reply.input :content
    = f.nested :owner do |owner|
      - capture_haml do
        = owner.input :name
        = owner.input :email
    = f.input :uuid
    = f.submit value: "Submit"

I don't know enough about internals for formular to say if this can be fixed internally, but I think this might be the right spot? https://github.com/trailblazer/formular/blob/master/lib/formular/html_block.rb

konung commented 6 years ago

@konung Can you add an simple example in a branch in the demo repo too?

I set it up (using the capture_haml approach above), but can't pull request to a non-existent branch on your repo - https://github.com/konung/gemgem-sinatra/tree/formular-haml-bootstrap4

Screenshot: https://user-images.githubusercontent.com/72493/46496670-417a4780-c7de-11e8-845c-5aaa9236be04.png