Module: CoreHelper::HTML
- Included in:
- CoreHelper
- Defined in:
- lib/helpers/core_helper.rb
Instance Method Summary collapse
-
#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>.
-
#html_options_from_hash(options = {}) ⇒ Object
Converts hash of options into a string of HTML property=“value” pairs.
-
#javascript_include_tag(*sources) ⇒ Object
Creates script tag to javascript(s) (options are support as the last argument).
-
#javascript_tag(name, options = {}) ⇒ Object
Creates a javascript script tag.
-
#short_tag_names ⇒ Object
Returns array of tag names that can be closed with /> instead of </tagname>.
-
#stylesheet_link_tag(*sources) ⇒ Object
Creates link tag to stylesheet(s) (options are support as the last argument).
-
#stylesheet_tag(name, options = {}) ⇒ Object
Creates a stylesheet link tag.
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 content_tag(name, *arguments, &block) name = name.to_s = Hash === arguments.last ? arguments.pop : {} properties = () 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 (={}) .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? = Hash === sources.last ? sources.pop : {} sources.collect { |e| javascript_tag(e, ) }.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, ={}) name += '.js' unless name =~ /\./ .merge! :src => "/javascripts/#{name}", :type => 'text/javascript' content_tag :script, end |
#short_tag_names ⇒ Object
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 |
#stylesheet_link_tag(*sources) ⇒ Object
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? = Hash === sources.last ? sources.pop : {} result = sources.map { |e| stylesheet_tag(e, ) } 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, ={}) name += '.css' unless name =~ /\./ = { :href => "/stylesheets/#{name}", :rel => 'stylesheet', :type => 'text/css' }.merge() content_tag :link, end |