Module: Sinatra::Tags::Helpers
- Defined in:
- lib/sinatra/tags.rb
Constant Summary collapse
- MULTI_LINE_TAGS =
Tags that should be rendered in multiple lines, like…
<body> <snip...> </body>
%w( a address applet bdo big blockquote body button caption center colgroup dd dir div dl dt fieldset form frameset head html iframe map noframes noscript object ol optgroup pre script select small style table tbody td tfoot th thead title tr tt ul )
- SELF_CLOSING_TAGS =
Self closing tags, like…
<hr> or <hr />
%w( area base br col frame hr img input link meta param )
- SINGLE_LINE_TAGS =
Tags that should be rendered in a single line, like…
<h1>Header</h1>
%w( abbr acronym b cite code del dfn em h1 h2 h3 h4 h5 h6 i kbd label legend li option p q samp span strong sub sup var )
- BOOLEAN_ATTRIBUTES =
Boolean attributes, ie: attributes like…
<input type="text" selected="selected"...>
%w( selected checked disabled readonly multiple )
Instance Method Summary collapse
-
#tag(*args, &block) ⇒ Object
Returns markup for tag name.
Instance Method Details
#tag(*args, &block) ⇒ Object
Returns markup for tag name.
Optionally contents may be passed, which is literal content for spanning tags such as textarea
, etc.
A hash of attrs may be passed as the second or third argument.
Self closing tags such as <br/>
, <input/>
, etc are automatically closed depending on output format, HTML vs XHTML.
Boolean attributes like “selected
”, “checked
” etc, are mirrored or removed when true
or false
.
Examples
Self closing tags:
tag(:br)
# => <br> or <br/> if XHTML
tag(:hr, :class => "space")
# => <hr class="space">
Multi line tags:
tag(:div)
# => <div></div>
tag(:div, 'content')
# => <div>content</div>
tag(:div, 'content', :id => 'comment')
# => <div id="comment">content</div>
tag(:div, :id => 'comment') # NB! no content
# => <div id="comment"></div>
Single line tags:
tag(:h1,'Header')
# => <h1>Header</h1>
tag(:abbr, 'WHO', :title => "World Health Organization")
# => <abbr title="World Health Organization">WHO</abbr>
Working with blocks
tag(:div) do
tag(:p, 'Hello World')
end
# => <div><p>Hello World</p></div>
<% tag(:div) do %>
<p>Paragraph 1</p>
<%= tag(:p, 'Paragraph 2') %>
<p>Paragraph 3</p>
<% end %>
# =>
<div>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>
</div>
# NB! ignored tag contents if given a block
<% tag(:div, 'ignored tag-content') do %>
<%= tag(:label, 'Comments:', :for => :comments) %>
<%= tag(:textarea,'textarea contents', :id => :comments) %>
<% end %>
# =>
<div>
<label for="comments">Comments:</label>
<textarea id="comments">
textarea contents
</textarea>
</div>
Boolean attributes:
tag(:input, :type => :checkbox, :checked => true)
# => <input type="checkbox" checked="checked" />
tag(:option, 'Sinatra', :value => "1" :selected => true)
# => <option value="1">Sinatra</option>
tag(:option, 'PHP', :value => "0" :selected => false)
# => <option value="0">PHP</option>
409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/sinatra/tags.rb', line 409 def tag(*args, &block) name = args.first attrs = args.last.is_a?(::Hash) ? args.pop : {} newline = attrs[:newline] # save before it gets tainted tag_content = block_given? ? capture_html(&block) : args[1] # content if self_closing_tag?(name) tag_html = self_closing_tag(name, attrs) else tag_html = open_tag(name, attrs) + tag_contents_for(name, tag_content, newline) + closing_tag(name) end block_is_template?(block) ? concat_content(tag_html) : tag_html end |