Module: Erector::Convenience

Included in:
Widget
Defined in:
lib/erector/convenience.rb

Instance Method Summary collapse

Instance Method Details

#css(href, options = {}) ⇒ Object

Convenience method to emit a css file link, which looks like this: <link href=“erector.css” rel=“stylesheet” type=“text/css” /> The parameter is the full contents of the href attribute, including any “.css” extension.

If you want to emit raw CSS inline, use the #style method instead.



42
43
44
# File 'lib/erector/convenience.rb', line 42

def css(href, options = {})
  link({:rel => 'stylesheet', :type => 'text/css', :href => href}.merge(options))
end

#dom_idObject

makes a unique id based on the widget’s class name and object id that you can use as the HTML id of an emitted element



54
55
56
# File 'lib/erector/convenience.rb', line 54

def dom_id
  "#{self.class.name.gsub(/:+/,"_")}_#{self.object_id}"
end

#join(array, separator) ⇒ Object

Emits the result of joining the elements in array with the separator. The array elements and separator can be Erector::Widget objects, which are rendered, or strings, which are html-escaped and output.



26
27
28
29
30
31
32
33
34
35
# File 'lib/erector/convenience.rb', line 26

def join(array, separator)
  first = true
  array.each do |widget_or_text|
    if !first
      text separator
    end
    first = false
    text widget_or_text
  end
end

#to_pretty(options = {}) ⇒ Object

Render (like to_html) but adding newlines and indentation. You may just want to call to_html(:prettyprint => true) so you can pass in other rendering options as well.



6
7
8
# File 'lib/erector/convenience.rb', line 6

def to_pretty(options = {})
  to_html(options.merge(:prettyprint => true))
end

#to_text(options = {}) ⇒ Object

Render (like to_html) but stripping all tags and inserting some appropriate formatting. Currently we format p, br, ol, ul, and li tags.



13
14
15
16
17
18
19
20
21
# File 'lib/erector/convenience.rb', line 13

def to_text(options = {})
  # TODO: make text output a first class rendering strategy, like HTML is now,
  # so we can do things like nested lists and numbered lists
  html = to_html(options.merge(:prettyprint => false))
  html.gsub!(/^<p[^>]*>/m, '')
  html.gsub!(/(<(ul|ol)>)?<li>/, "\n* ")
  html.gsub!(/<(\/?(ul|ol|p|br))[^>]*( \/)?>/, "\n")
  CGI.unescapeHTML(html.gsub(/<[^>]*>/, ''))
end

#url(href, options = {}) ⇒ Object

Convenience method to emit an anchor tag whose href and text are the same, e.g. <a href=“example.com”>example.com</a>



48
49
50
# File 'lib/erector/convenience.rb', line 48

def url(href, options = {})
  a href, ({:href => href}.merge(options))
end