Module: Herbie::Helpers

Defined in:
lib/herbie/erb_helpers.rb,
lib/herbie/html_helpers.rb,
lib/herbie/generic_helpers.rb

Instance Method Summary collapse

Instance Method Details



96
97
98
99
100
101
102
103
# File 'lib/herbie/html_helpers.rb', line 96

def link_to(href, text=nil, attrs={}, &block)
  attrs = {:href => href}.merge(attrs)
  if block_given?
    erb_concat "#{tag :a, attrs}#{capture_erb(&block)}</a>"
  else
    "#{tag :a, attrs}#{text ||= attrs[:href]}</a>"
  end
end

#script(source = nil, options = {}, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/herbie/html_helpers.rb', line 58

def script(source=nil, options={}, &block)
  attrs = {
    :type    => "text/javascript",
    :charset => "utf-8"
  }

  if block_given?
    erb_concat "#{tag :script, attrs}\n#{capture_erb(&block)}\n</script>\n"
  else
    source = "/javascripts/#{source}" unless source.nil? || source.match(/^\/{1,2}|^http:\/\/|^https:\/\//)
    source = source.sub(/.js$/, '.min.js') if source && options[:minified] && !source.match(/.min.js$/)
    attrs = attrs.merge({:src => source})
    "#{tag :script, attrs}</script>"
  end
end

#style(href = nil, attrs = {}, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/herbie/html_helpers.rb', line 74

def style(href=nil, attrs={}, &block)
  default_attrs = {
    :rel  => "stylesheet",
    :type => "text/css"
  }

  if block_given?
    default_attrs.delete :rel
    erb_concat "#{tag :style, default_attrs.merge(attrs)}\n#{capture_erb(&block)}\n</style>\n"
  else
    href = "/stylesheets/#{href}" unless href.match(/^\/{1,2}|^http:\/\/|^https:\/\//)

    if attrs[:minified]
      attrs.delete :minified
      href = href.sub(/.css$/, '.min.css') if href && !href.match(/.min.css$/)
    end

    attrs = default_attrs.merge({:href => href}.merge(attrs))
    "#{tag :link, attrs}"
  end
end

#tag(name, attrs = {}, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/herbie/html_helpers.rb', line 3

def tag(name, attrs={}, &block)
  if block_given?
    erb_concat "<#{name}#{' ' + attributes(attrs) unless attrs.nil? || attrs.empty? || attributes(attrs).empty?}>\n#{capture_erb(&block)}\n</#{name}>\n"
  elsif !attrs[:content].nil?
    case name
    when :meta
      "<#{name}#{' ' + attributes(attrs) unless attrs.empty? || attributes(attrs).empty?}>"
    else
      content = attrs.delete :content
      "<#{name}#{' ' + attributes(attrs) unless attrs.empty? || attributes(attrs).empty?}>#{content}</#{name}>"
    end
  else
    case name
    when :select
      option_tags = attrs.delete(:options).collect do |option|
        if option.key? :label
          option[:content] = option.delete(:options).collect {|o| tag :option, o}.join
          tag :optgroup, option
        else
          tag :option, option
        end
      end.join
      "<#{name}#{' ' + attributes(attrs) unless attrs.empty? || attributes(attrs).empty?}>#{option_tags}</#{name}>"
    when :span
      "<#{name}#{' ' + attributes(attrs) unless attrs.empty? || attributes(attrs).empty?}></#{name}>"
    else
      "<#{name}#{' ' + attributes(attrs) unless attrs.empty? || attributes(attrs).empty?}>"
    end
  end
end

#tags(name, collection, attrs = {}, &block) ⇒ Object

work in progress



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/herbie/html_helpers.rb', line 35

def tags(name, collection, attrs={}, &block)
  cycle = attrs.delete :cycle
  result = ""
  collection.each_with_index do |item, i|
    a = attrs.dup

    unless cycle.nil?
      c = a.delete :class
      classes = []
      classes << c unless c.nil?
      classes << cycle[:even] if i.even? && cycle.key?(:even)
      classes << cycle[:odd] if i.odd? && cycle.key?(:odd)
      a[:class] = classes.join " " unless classes.empty?
    end

    result += tag name, a do
      block.call(item)
    end
    result += "\n"
  end
  result
end