Module: CoreHelper::HTML

Included in:
CoreHelper
Defined in:
lib/helpers/core_helper.rb

Instance Method Summary collapse

Instance Method Details

#content_tag(name, *arguments, &block) ⇒ Object

Creates a tag with content and properties Example:

content_tag(:div, "Hello", :id => 'greeting')
  -- generates --
<div id="greeting">Hello</div>


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/helpers/core_helper.rb', line 8

def (name, *arguments, &block)
  name    = name.to_s
  options = Hash === arguments.last ? arguments.pop : {}
  properties = html_options_from_hash(options)

  result = ["<#{name}"]
  result << ' ' + properties unless properties.empty?

  if short_tag_names.include?(name)
    result << ' />'
  else
    result << '>'
    result << arguments.join
    result << capture(&block) if block_given?
    result << "</#{name}>"  
  end

  result = result.join('')
  concat(result, block.binding) if block_given?
  result
end

#html_options_from_hash(options = {}) ⇒ Object

Converts hash of options into a string of HTML property=“value” pairs



38
39
40
41
42
# File 'lib/helpers/core_helper.rb', line 38

def html_options_from_hash(options={})
  options.map do |key, value|
    %Q{#{key}="#{value}"}
  end.sort.join(' ')
end

#javascript_include_tag(*sources) ⇒ Object

Creates script tag to javascript(s) (options are support as the last argument)



66
67
68
69
70
# File 'lib/helpers/core_helper.rb', line 66

def javascript_include_tag(*sources)
  return nil if sources.empty?
  options = Hash === sources.last ? sources.pop : {}
  sources.collect { |e| javascript_tag(e, options) }.join("\n")
end

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

Creates a javascript script tag



73
74
75
76
77
78
79
# File 'lib/helpers/core_helper.rb', line 73

def javascript_tag(name, options={})
 name += '.js' unless name =~ /\./
 options.merge! :src => "/javascripts/#{name}",
   :type => 'text/javascript'
 
  :script, options 
end

#short_tag_namesObject

Returns array of tag names that can be closed with /> instead of </tagname>



32
33
34
# File 'lib/helpers/core_helper.rb', line 32

def short_tag_names
  %w(input br link hr img)
end

Creates link tag to stylesheet(s) (options are support as the last argument)



46
47
48
49
50
51
# File 'lib/helpers/core_helper.rb', line 46

def stylesheet_link_tag(*sources)
  return nil if sources.empty?
  options = Hash === sources.last ? sources.pop : {}
  result  = sources.map { |e| stylesheet_tag(e, options) }
  result.join("\n")
end

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

Creates a stylesheet link tag



54
55
56
57
58
59
60
61
62
# File 'lib/helpers/core_helper.rb', line 54

def stylesheet_tag(name, options={})
  name += '.css' unless name =~ /\./
  link_options = {
    :href => "/stylesheets/#{name}",
    :rel => 'stylesheet', :type => 'text/css'
  }.merge(options)
  
   :link, link_options  
end