diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index 8a20274d..df6a29dd 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -23,22 +23,52 @@ module Haml input.gsub(/\n/, ' ').gsub(/\r/, '') end - # Takes an array and a block and iterates over the array, - # yielding each element to the block and putting the - # result into
  • elements, creating a list - # of the results of the block. For example: + # Takes an Enumerable object and a block + # and iterates over the object, + # yielding each element to a Haml block + # and putting the result into
  • elements. + # This creates a list of the results of the block. + # For example: # - # list_of([['hello'], ['yall']]) { |i| i[0] } - # or - # list_of(['hello', 'yall']) + # = list_of([['hello'], ['yall']]) do |i| + # = i[0] # - # Both produce: + # Produces: # #
  • hello
  • #
  • yall
  • # - def list_of(array) # :yields: item - (array.collect { |i| "
  • #{yield(i)}
  • " }).join("\n") + # And + # + # = list_of({:title => 'All the stuff', :description => 'A book about all the stuff.'}) do |key, val| + # %h3= key.humanize + # %p= val + # + # Produces: + # + #
  • + #

    Title

    + #

    All the stuff

    + #
  • + #
  • + #

    Description

    + #

    A book about all the stuff.

    + #
  • + # + def list_of(array, &block) # :yields: item + to_return = array.collect do |i| + result = capture_haml(i, &block) + + if result.count("\n") > 1 + result.gsub!("\n", "\n ") + result = "\n #{result.strip}\n" + else + result.strip! + end + + "
  • #{result}
  • " + end + to_return.join("\n") end # Increments the number of tabs the buffer automatically adds diff --git a/test/helper_test.rb b/test/helper_test.rb index cbd86474..ccc76b3f 100644 --- a/test/helper_test.rb +++ b/test/helper_test.rb @@ -32,8 +32,10 @@ class HelperTest < Test::Unit::TestCase end def test_list_of_should_render_correctly - assert_equal("
  • 1
  • \n
  • 2
  • ", (list_of([1, 2]) { |i| i.to_s})) - assert_equal("
  • 1
  • ", (list_of([[1]]) { |i| i.first})) + assert_equal("
  • 1
  • \n
  • 2
  • \n", render("= list_of([1, 2]) do |i|\n = i")) + assert_equal("
  • 1
  • \n", render("= list_of([[1]]) do |i|\n = i.first")) + assert_equal("
  • \n

    Fee

    \n

    A word!

    \n
  • \n
  • \n

    Fi

    \n

    A word!

    \n
  • \n
  • \n

    Fo

    \n

    A word!

    \n
  • \n
  • \n

    Fum

    \n

    A word!

    \n
  • \n", + render("= list_of(['Fee', 'Fi', 'Fo', 'Fum']) do |title|\n %h1= title\n %p A word!")) end def test_buffer_access diff --git a/test/results/helpers.xhtml b/test/results/helpers.xhtml index c40ef56c..8f1131b3 100644 --- a/test/results/helpers.xhtml +++ b/test/results/helpers.xhtml @@ -47,3 +47,4 @@ click

    baz

    boom

    foo +
  • google
  • diff --git a/test/templates/helpers.haml b/test/templates/helpers.haml index a97164c7..304c40e3 100644 --- a/test/templates/helpers.haml +++ b/test/templates/helpers.haml @@ -34,3 +34,6 @@ click - buffer.tabulation = 10 %p boom - concat "foo\n" +- buffer.tabulation = 0 += list_of({:google => 'http://www.google.com'}) do |name, link| + %a{ :href => link }= name