trailblazer / formular

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

Weird behaviour for rendering element without content #6

Closed blelump closed 8 years ago

blelump commented 8 years ago

Hi @fran-worley ,

consider such class:

     class Span < Formular::Elements::Container
        tag :span
        set_default :class, ['icon-controller-record']
      end

When rendered without any content, eg Span.call() it returns "<span class=\"icon-controller-record\">", whereas Span.call(content: " ") returns "<span class=\"icon-controller-record\"> </span>" .

fran-worley commented 8 years ago

This is actually intentional to enable you to build your content without blocks.

The Container module provides an end method which gives you the closing tag.

I can see that being a pain when you want empty spans though.

fran-worley commented 8 years ago

Whilst I'm thinking, you could do this for now:

class Span < Formular::Elements::Container
  tag :span
  set_default :class, ['icon-controller-record']
  self.html_context = :with_content
end
fran-worley commented 8 years ago

That forces your element to render the start & end tag regardless of content.

blelump commented 8 years ago

@fran-worley , fair enough for me ! Empty span occurs due to the frontend design – it is a wrapper for radio checked/unchecked state (see image given in https://github.com/trailblazer/formular/issues/5 )

fran-worley commented 8 years ago

Yeah I've been thinking some more about this and am favouring rendering the complete 'empty' tag by default (most common use case) and providing a start method if you just want the opener. e.g.

# blockless example:

form = builder.form(action: '/questions/13')
html = form.start
html << form.textarea(:body).to_s
html << form.select(:public, collection: collection_array).to_s
html << form.input(:body).to_s
html << form.end
html.must_equal %(<form method="post" accept-charset=\"utf-8\" action="/questions/13"><input name=\"utf8\" type=\"hidden\" value=\"✓\"/><div class="form-group"><textarea name="comment[body]" id="comment_body" class="form-control">Something exciting</textarea></div><div class="form-group"><select name="comment[public]" id="comment_public" class="form-control"><option value="1">Yes</option><option value="0">No</option></select></div><div class="form-group"><input name="comment[body]" id="comment_body" value="Something exciting" type="text" class="form-control"/></div></form>)
fran-worley commented 8 years ago

Going to close this now as the default behaviour is no longer weird.