Module: AnyView::Helpers::TagHelpers

Defined in:
lib/any_view/tag_helpers.rb

Instance Method Summary collapse

Instance Method Details

#content_tag(*args, &block) ⇒ Object

Creates an html tag with given name, content and options parameters: content_tag(name, content=nil, options={}, &block) Writes directly to the buffer

Examples:

content_tag(:p, "hello", :class => 'light')
content_tag(:p, :class => 'dark') do ... end

18
19
20
21
22
23
24
# File 'lib/any_view/tag_helpers.rb', line 18

def (*args, &block)
  name = args.first
  options = args.extract_options!
  tag_html = block_given? ? capture_content(options, &block) : args[1]
  tag_result = tag(name, options.merge(:content => tag_html))
  block.nil? ? tag_result : concat_content(tag_result)
end

#input_tag(type, options = {}) ⇒ Object

Creates an html input field with given type and options input_tag :text, :class => “test”


6
7
8
9
10
# File 'lib/any_view/tag_helpers.rb', line 6

def input_tag(type, options = {})
  options = options.dup
  options[:type] = type
  tag(:input, options)
end

#tag(name, options = {}) ⇒ Object

Creates an html tag with the given name and options

Examples:

tag(:br, :style => 'clear:both')
tag(:p, :content => "hello", :class => 'large')

Returns:

  • a string


31
32
33
34
35
36
37
# File 'lib/any_view/tag_helpers.rb', line 31

def tag(name, options={})
  content, open_tag = options.delete(:content), options.delete(:open)
  identity_tag_attributes.each { |attr| options[attr] = attr.to_s if options[attr]  }
  html_attrs = options.collect { |a, v| v.blank? ? nil : "#{a}=\"#{v}\"" }.compact.join(" ")
  base_tag = (!html_attrs.blank? ? "<#{name} #{html_attrs}" : "<#{name}")
  base_tag << (open_tag ? ">" : (content ? ">#{content}</#{name}>" : " />"))
end