Module: Tiny::Markup

Included in:
ActionViewAdditions, ErubisTemplating, HamlTemplating, Helpers
Defined in:
lib/tiny.rb

Overview

Provides basic markup generation support.

Instance Method Summary collapse

Instance Method Details

#cdata(content) ⇒ String

Appends a CDATA section to the content.

div do
  cdata 'foo'
end
# => <div>
<![CDATA[foo]]>
</div>

Returns:



141
142
143
144
# File 'lib/tiny.rb', line 141

def cdata(content)
  content = content.to_s.gsub(']]>', ']]]]><![CDATA[>')
  append! "<![CDATA[#{content}]]>"
end

#comment(content) ⇒ SafeString

Appends an HTML coment to the content.

div do
  comment 'foo'
end
# => <div>
  <!-- foo -->
</div>

Returns:



126
127
128
# File 'lib/tiny.rb', line 126

def comment(content)
  append! "<!-- #{content.to_s.gsub(/-(?=-)/, '- ')} -->"
end

#doctypeString

Appends the doctype to the content

with_buffer do
  doctype
  html_tag(:html) do
    ...
  end
end
# => <!DOCTYPE html>
<html>
  ...
</html>

Returns:



161
162
163
# File 'lib/tiny.rb', line 161

def doctype
  append! "<!DOCTYPE html>"
end

#html_tag(name, attrs_or_content = {}, attrs = nil) { ... } ⇒ String

Generates an HTML tag or structured HTML markup

This method is the basis for defining html helper functions or constructing html markup using pure ruby.

HTML attributes are HTML-escaped and tags are explicitly or self closed depeding on the tag name.

Calls to markup generating methods within the content block are appended to the tag’s content. See Tiny::Markup and HTML.

Content blocks originating from HAML or ERB templates are correctly captured and handled.

Examples:

Attribute mapping


html_tag(:link, :href => 'my-styles.css')
# => <link href="my-styles.css" />
html_tag(:li, 'Bicycle', :class => ['with-discount', 'in-stock'])
# => <li class="with-discount in-stock">Bicycle</li>
html_tag(:textarea, :disabled => true)
# => <textarea disabled></textarea>
html_tag(:textarea, :disabled => false)
# => <textarea></textarea>
html_tag(:textarea, :disabled => nil)
# => <textarea></textarea>

HTML-escaping


html_tag(:a, 'Art&Copy', :href => '/art&copy')
# => <a href="/art&amp;copy">Art&amp;copy</a>

Tag closing


html_tag(:p)    # => <p></p>
html_tag(:link) # => <link />

Content block


html_tag(:ul) do
  html_tag(:li, 'Cheese')
  html_tag(:li, 'Ham')
  html_tag(:li, 'Milk')
end
# => <ul>
  <li>Cheese</li>
  <li>Haml</li>
  <li>Milk</li>
</ul>

html_tag(:p) do
  text 'Neque porro quisquam est qui dolorem...'
  'this string will be ignored'
end
# => <p>
Neque porro quisquam est qui dolorem...
</p>

ERB blocks

# application_helper.rb
module ApplicationHelper
  def my_form(url, &block)
    html_tag(:form, :action => url) do
      ...
      html_tag(:fieldset, &block)
      ...
    end
  end

  def text_input(name, value)
    html_tag(:input, :type => 'text', :name => name, :value => value)
  end
end

# form.erb
<%= my_form '/login' do %>
  <%= text_input 'email', @email %>
<% end %>
# => <form action="/login">
  ...
  <fieldset>
    <input type="text" name="email" value="[email protected]" />
  </fieldset>
  ...
</form>

Parameters:

  • name (Symbol, String)

    The name of the tag.

  • attrs_or_content (Hash, String) (defaults to: {})

    Tag’s attributes or content.

  • attrs (Hash) (defaults to: nil)

    Tag’s attributes if content string passed.

Yields:

  • Content block.

Returns:



111
112
113
# File 'lib/tiny.rb', line 111

def html_tag(name, attrs_or_content = {}, attrs = nil, &block)
  append! Tag.new(name, attrs_or_content, attrs).render(&block)
end