mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
Changed/expanded functionality of list_of.
git-svn-id: svn://hamptoncatlin.com/haml/branches/edge@171 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
parent
6bdf315c4f
commit
8ef9704c58
4 changed files with 48 additions and 12 deletions
|
@ -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 <tt><li></tt> 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 <tt><li></tt> 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:
|
||||
#
|
||||
# <li>hello</li>
|
||||
# <li>yall</li>
|
||||
#
|
||||
def list_of(array) # :yields: item
|
||||
(array.collect { |i| "<li>#{yield(i)}</li>" }).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:
|
||||
#
|
||||
# <li>
|
||||
# <h3>Title</h3>
|
||||
# <p>All the stuff</p>
|
||||
# </li>
|
||||
# <li>
|
||||
# <h3>Description</h3>
|
||||
# <p>A book about all the stuff.</p>
|
||||
# </li>
|
||||
#
|
||||
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
|
||||
|
||||
"<li>#{result}</li>"
|
||||
end
|
||||
to_return.join("\n")
|
||||
end
|
||||
|
||||
# Increments the number of tabs the buffer automatically adds
|
||||
|
|
|
@ -32,8 +32,10 @@ class HelperTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_list_of_should_render_correctly
|
||||
assert_equal("<li>1</li>\n<li>2</li>", (list_of([1, 2]) { |i| i.to_s}))
|
||||
assert_equal("<li>1</li>", (list_of([[1]]) { |i| i.first}))
|
||||
assert_equal("<li>1</li>\n<li>2</li>\n", render("= list_of([1, 2]) do |i|\n = i"))
|
||||
assert_equal("<li>1</li>\n", render("= list_of([[1]]) do |i|\n = i.first"))
|
||||
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",
|
||||
render("= list_of(['Fee', 'Fi', 'Fo', 'Fum']) do |title|\n %h1= title\n %p A word!"))
|
||||
end
|
||||
|
||||
def test_buffer_access
|
||||
|
|
|
@ -47,3 +47,4 @@ click
|
|||
<p>baz</p>
|
||||
<p>boom</p>
|
||||
foo
|
||||
<li><a href='http://www.google.com'>google</a></li>
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue