Module Haml::Helpers
In: lib/haml/helpers.rb
lib/haml/helpers/action_view_extensions.rb

This module contains various helpful methods to make it easier to do various tasks. Haml::Helpers is automatically included in the context that a Haml template is parsed in, so all these methods are at your disposal from within the template.

Methods

Included Modules

ActionViewExtensions

Classes and Modules

Module Haml::Helpers::ActionViewExtensions

Public Class methods

Returns whether or not ActionView is installed on the system.

[Source]

    # File lib/haml/helpers.rb, line 16
16:     def self.action_view?
17:       @@action_view_defined
18:     end

Public Instance methods

Captures the result of the given block of Haml code, gets rid of the excess indentation, and returns it as a string. For example, after the following,

  .foo
    - foo = capture_haml(13) do |a|
      %p= a

the local variable foo would be assigned to "<p>13</p>\n".

[Source]

     # File lib/haml/helpers.rb, line 201
201:     def capture_haml(*args, &block)
202:       capture_haml_with_buffer(buffer.buffer, *args, &block)
203:     end

Isolates the whitespace-sensitive tags in the string and uses preserve to convert any endlines inside them into HTML entities for endlines.

[Source]

    # File lib/haml/helpers.rb, line 22
22:     def find_and_preserve(input)
23:       input = input.to_s
24:       input.scan(/<(textarea|code|pre)[^>]*>(.*?)<\/\1>/im) do |tag, contents|
25:         input = input.gsub(contents, preserve(contents))
26:       end
27:       input
28:     end
flatten(input)

Alias for preserve

Returns a hash containing default assignments for the xmlns and xml:lang attributes of the html HTML element. It also takes an optional argument for the value of xml:lang and lang, which defaults to ‘en-US’. For example,

  %html{html_attrs}

becomes

  <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en-US' lang='en-US'>

[Source]

     # File lib/haml/helpers.rb, line 99
 99:     def html_attrs(lang = 'en-US')
100:       {:xmlns => "http://www.w3.org/1999/xhtml", 'xml:lang' => lang, :lang => lang}
101:     end

Takes an Enumerable object and a block and iterates over the object, yielding each element to a Haml block and putting the result into <li> elements. This creates a list of the results of the block. For example:

  = list_of([['hello'], ['yall']]) do |i|
    = i[0]

Produces:

  <li>hello</li>
  <li>yall</li>

And

  = list_of({:title => 'All the stuff', :description => 'A book about all the stuff.'}) do |key, val|
    %h3= key.humanize
    %p= val

Produces:

  <li>
    <h3>Title</h3>
    <p>All the stuff</p>
  </li>
  <li>
    <h3>Description</h3>
    <p>A book about all the stuff.</p>
  </li>

[Source]

    # File lib/haml/helpers.rb, line 71
71:     def list_of(array, &block) # :yields: item
72:       to_return = array.collect do |i|
73:         result = capture_haml(i, &block)
74:         
75:         if result.count("\n") > 1
76:           result.gsub!("\n", "\n  ")
77:           result = "\n  #{result.strip}\n"
78:         else
79:           result.strip!
80:         end
81:         
82:         "<li>#{result}</li>"
83:       end
84:       to_return.join("\n")
85:     end

Creates an HTML tag with the given name and optionally text and attributes. Can take a block that will be executed between when the opening and closing tags are output. If the block is a Haml block or outputs text using puts, the text will be properly indented.

For example,

  open :table do
    open :tr do
      open :td, {:class => 'cell'} do
        open :strong, "strong!"
        puts "data"
      end
      open :td do
        puts "more_data"
      end
    end
  end

outputs

  <table>
    <tr>
      <td class='cell'>
        <strong>
          strong!
        </strong>
        data
      </td>
      <td>
        more_data
      </td>
    </tr>
  </table>

[Source]

     # File lib/haml/helpers.rb, line 252
252:     def open(name, attributes = {}, alt_atts = {}, &block)
253:       text = nil
254:       if attributes.is_a? String
255:         text = attributes
256:         attributes = alt_atts
257:       end
258: 
259:       puts "<#{name}#{buffer.build_attributes(attributes)}>"
260:       tab_up
261:         # Print out either the text (using push_text) or call the block and add an endline
262:         if text
263:           puts(text)
264:         elsif block
265:           block.call
266:         end
267:       tab_down
268:       puts "</#{name}>"
269:       nil
270:     end

Prepends the given character to the beginning of the Haml block, with no whitespace between. For example:

  = precede '*' do
    %span.small Not really

Produces:

  *<span class='small'>Not really</span>

[Source]

     # File lib/haml/helpers.rb, line 169
169:     def precede(char, &block)
170:       "#{char}#{capture_haml(&block).chomp}\n"
171:     end

Takes any string, finds all the endlines and converts them to HTML entities for endlines so they‘ll render correctly in whitespace-sensitive tags without screwing up the indentation.

[Source]

    # File lib/haml/helpers.rb, line 33
33:     def preserve(input)      
34:       input.gsub(/\n/, '&#x000A;').gsub(/\r/, '')
35:     end

Outputs text directly to the Haml buffer, with the proper tabulation

[Source]

     # File lib/haml/helpers.rb, line 206
206:     def puts(text = "")
207:       buffer.buffer << ('  ' * buffer.tabulation) << text.to_s << "\n"
208:       nil
209:     end

Appends the given character to the end of the Haml block, with no whitespace between. For example:

  click
  = succeed '.' do
    %a{:href=>"thing"} here

Produces:

  click
  <a href='thing'>here</a>.

[Source]

     # File lib/haml/helpers.rb, line 186
186:     def succeed(char, &block)
187:       "#{capture_haml(&block).chomp}#{char}\n"
188:     end

Surrounds the given block of Haml code with the given characters, with no whitespace in between. For example:

  = surround '(', ')' do
    %a{:href => "food"} chicken

Produces:

  (<a href='food'>chicken</a>)

and

  = surround '*' do
    %strong angry

Produces:

  *<strong>angry</strong>*

[Source]

     # File lib/haml/helpers.rb, line 151
151:     def surround(front, back = nil, &block)
152:       back ||= front
153:       output = capture_haml(&block)
154:       
155:       "#{front}#{output.chomp}#{back}\n"
156:     end

Increments the number of tabs the buffer automatically adds to the lines of the template.

See tab_up.

[Source]

     # File lib/haml/helpers.rb, line 127
127:     def tab_down(i = 1)
128:       buffer.tabulation -= i
129:     end

Increments the number of tabs the buffer automatically adds to the lines of the template. For example:

  %h1 foo
  - tab_up
  %p bar
  - tab_down
  %strong baz

Produces:

  <h1>foo</h1>
    <p>bar</p>
  <strong>baz</strong>

[Source]

     # File lib/haml/helpers.rb, line 119
119:     def tab_up(i = 1)
120:       buffer.tabulation += i
121:     end

[Validate]