public
Rubygem
Fork of nex3/haml
Description: HTML Abstraction Markup Language - A Markup Haiku
Homepage: http://haml.hamptoncatlin.com
Clone URL: git://github.com/chriseppstein/haml.git
Search Repo:
Changed/expanded functionality of list_of.


git-svn-id: svn://hamptoncatlin.com/haml/branches/edge@171 
7063305b-7217-0410-af8c-cdc13e5119b9
nex3 (author)
Mon Nov 27 17:44:40 -0800 2006
commit  8ef9704c583d60a94d88550cf802fb3c5af64613
tree    738df1ca13e25d31cf9f0c2588bdf5fb5632ea9f
parent  6bdf315c4f224ff0ecfa0771643efa5e58d7e55c
...
23
24
25
26
27
28
29
 
 
 
 
 
 
30
31
32
33
 
 
34
35
 
36
37
38
39
40
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
43
44
...
23
24
25
 
 
 
 
26
27
28
29
30
31
32
 
 
 
33
34
35
 
36
37
38
39
40
 
 
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
0
@@ -23,22 +23,52 @@ module Haml
0
       input.gsub(/\n/, '
').gsub(/\r/, '')
0
     end
0
 
0
- # Takes an array and a block and iterates over the array,
0
- # yielding each element to the block and putting the
0
- # result into <tt><li></tt> elements, creating a list
0
- # of the results of the block. For example:
0
+ # Takes an Enumerable object and a block
0
+ # and iterates over the object,
0
+ # yielding each element to a Haml block
0
+ # and putting the result into <tt><li></tt> elements.
0
+ # This creates a list of the results of the block.
0
+ # For example:
0
     #
0
- # list_of([['hello'], ['yall']]) { |i| i[0] }
0
- # or
0
- # list_of(['hello', 'yall'])
0
+ # = list_of([['hello'], ['yall']]) do |i|
0
+ # = i[0]
0
     #
0
- # Both produce:
0
+ # Produces:
0
     #
0
     # <li>hello</li>
0
     # <li>yall</li>
0
     #
0
- def list_of(array) # :yields: item
0
- (array.collect { |i| "<li>#{yield(i)}</li>" }).join("\n")
0
+ # And
0
+ #
0
+ # = list_of({:title => 'All the stuff', :description => 'A book about all the stuff.'}) do |key, val|
0
+ # %h3= key.humanize
0
+ # %p= val
0
+ #
0
+ # Produces:
0
+ #
0
+ # <li>
0
+ # <h3>Title</h3>
0
+ # <p>All the stuff</p>
0
+ # </li>
0
+ # <li>
0
+ # <h3>Description</h3>
0
+ # <p>A book about all the stuff.</p>
0
+ # </li>
0
+ #
0
+ def list_of(array, &block) # :yields: item
0
+ to_return = array.collect do |i|
0
+ result = capture_haml(i, &block)
0
+
0
+ if result.count("\n") > 1
0
+ result.gsub!("\n", "\n ")
0
+ result = "\n #{result.strip}\n"
0
+ else
0
+ result.strip!
0
+ end
0
+
0
+ "<li>#{result}</li>"
0
+ end
0
+ to_return.join("\n")
0
     end
0
 
0
     # Increments the number of tabs the buffer automatically adds
...
32
33
34
35
36
 
 
 
 
37
38
39
...
32
33
34
 
 
35
36
37
38
39
40
41
0
@@ -32,8 +32,10 @@ class HelperTest < Test::Unit::TestCase
0
   end
0
 
0
   def test_list_of_should_render_correctly
0
- assert_equal("<li>1</li>\n<li>2</li>", (list_of([1, 2]) { |i| i.to_s}))
0
- assert_equal("<li>1</li>", (list_of([[1]]) { |i| i.first}))
0
+ assert_equal("<li>1</li>\n<li>2</li>\n", render("= list_of([1, 2]) do |i|\n = i"))
0
+ assert_equal("<li>1</li>\n", render("= list_of([[1]]) do |i|\n = i.first"))
0
+ assert_equal("<li>\n <h1>Fee</h1>\n <p>A word!</p>\n</li>\n<li>\n <h1>Fi</h1>\n <p>A word!</p>\n</li>\n<li>\n <h1>Fo</h1>\n <p>A word!</p>\n</li>\n<li>\n <h1>Fum</h1>\n <p>A word!</p>\n</li>\n",
0
+ render("= list_of(['Fee', 'Fi', 'Fo', 'Fum']) do |title|\n %h1= title\n %p A word!"))
0
   end
0
 
0
   def test_buffer_access
...
47
48
49
 
...
47
48
49
50
0
@@ -47,3 +47,4 @@ click
0
 <p>baz</p>
0
                     <p>boom</p>
0
 foo
0
+<li><a href='http://www.google.com'>google</a></li>
...
34
35
36
 
 
 
...
34
35
36
37
38
39
0
@@ -34,3 +34,6 @@ click
0
 - buffer.tabulation = 10
0
 %p boom
0
 - concat "foo\n"
0
+- buffer.tabulation = 0
0
+= list_of({:google => 'http://www.google.com'}) do |name, link|
0
+ %a{ :href => link }= name

Comments

    No one has commented yet.