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.
Returns whether or not ActionView is installed on the system.
# File lib/haml/helpers.rb, line 16 16: def self.action_view? 17: @@action_view_defined 18: end
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".
# File lib/haml/helpers.rb, line 237 237: def capture_haml(*args, &block) 238: capture_haml_with_buffer(haml_buffer.buffer, *args, &block) 239: end
Isolates the whitespace-sensitive tags in the string and uses preserve to convert any endlines inside them into HTML entities for endlines.
# File lib/haml/helpers.rb, line 51 51: def find_and_preserve(input = '', &block) 52: return find_and_preserve(capture_haml(&block)) if block 53: 54: input = input.to_s 55: input.gsub(/<(textarea|code|pre)([^>]*)>(.*?)(<\/\1>)/im) do 56: "<#{$1}#{$2}>#{preserve($3)}</#{$1}>" 57: end 58: 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>
# File lib/haml/helpers.rb, line 288 288: def haml_tag(name, attributes = {}, alt_atts = {}, &block) 289: text = nil 290: if attributes.is_a? String 291: text = attributes 292: attributes = alt_atts 293: end 294: 295: if text.nil? && block.nil? 296: puts "<#{name}#{Haml::Precompiler.build_attributes(haml_buffer.options[:attr_wrapper], attributes)} />" 297: return nil 298: end 299: 300: puts "<#{name}#{Haml::Precompiler.build_attributes(haml_buffer.options[:attr_wrapper], attributes)}>" 301: unless text && text.empty? 302: tab_up 303: # Print out either the text (using push_text) or call the block and add an endline 304: if text 305: puts(text) 306: elsif block 307: block.call 308: end 309: tab_down 310: end 311: puts "</#{name}>" 312: nil 313: end
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'>
# File lib/haml/helpers.rb, line 135 135: def html_attrs(lang = 'en-US') 136: {:xmlns => "http://www.w3.org/1999/xhtml", 'xml:lang' => lang, :lang => lang} 137: end
Note: this does not need to be called when using Haml helpers normally in Rails.
Initializes the current object as though it were in the same context as a normal ActionView rendering using Haml. This is useful if you want to use the helpers in a context other than the normal setup with ActionView. For example:
context = Object.new class << context include Haml::Helpers end context.init_haml_helpers context.open :p, "Stuff"
# File lib/haml/helpers.rb, line 39 39: def init_haml_helpers 40: @haml_is_haml = true 41: @haml_stack = [Haml::Buffer.new] 42: nil 43: 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>
# File lib/haml/helpers.rb, line 107 107: def list_of(array, &block) # :yields: item 108: to_return = array.collect do |i| 109: result = capture_haml(i, &block) 110: 111: if result.count("\n") > 1 112: result.gsub!("\n", "\n ") 113: result = "\n #{result.strip}\n" 114: else 115: result.strip! 116: end 117: 118: "<li>#{result}</li>" 119: end 120: to_return.join("\n") 121: end
# File lib/haml/helpers.rb, line 315 315: def open(*args, &block) 316: warn "DEPRECATION WARNING:\nThe Haml #open helper is deprecated and will be removed in version 2.0.\nUse the #haml_tag method instead.\n" 317: haml_tag(*args, &block) 318: 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>
# File lib/haml/helpers.rb, line 205 205: def precede(char, &block) 206: "#{char}#{capture_haml(&block).chomp}\n" 207: 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.
# File lib/haml/helpers.rb, line 67 67: def preserve(input = '', &block) 68: return preserve(capture_haml(&block)) if block 69: 70: input.gsub(/\n/, '
').gsub(/\r/, '') 71: 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>.
# File lib/haml/helpers.rb, line 222 222: def succeed(char, &block) 223: "#{capture_haml(&block).chomp}#{char}\n" 224: 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>*
# File lib/haml/helpers.rb, line 187 187: def surround(front, back = nil, &block) 188: back ||= front 189: output = capture_haml(&block) 190: 191: "#{front}#{output.chomp}#{back}\n" 192: 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>
# File lib/haml/helpers.rb, line 155 155: def tab_up(i = 1) 156: haml_buffer.tabulation += i 157: end