mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
Added the list_for helper (as demoed at RailsConf)
git-svn-id: svn://hamptoncatlin.com/haml/trunk@44 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
parent
f770d4b444
commit
a815a23388
2 changed files with 27 additions and 0 deletions
|
@ -1,5 +1,7 @@
|
|||
module Haml
|
||||
module Helpers
|
||||
# Flatten will take any string, find all the endlines (via \n)
|
||||
# and convert them to html entities for endlines.
|
||||
def flatten(input)
|
||||
input.gsub(/\n/, '
').gsub(/\r/, '')
|
||||
end
|
||||
|
@ -18,5 +20,24 @@ module Haml
|
|||
def count_soft_tabs(line)
|
||||
line.index(/[^ ]/) ? [line.index(/[^ ]/)/2, line.strip] : []
|
||||
end
|
||||
|
||||
# List_for is a really nifty little helper that helps
|
||||
# cleanup your code. Basically, give it an array of
|
||||
# objects, and then pass in a block that tells how
|
||||
# what to put out, and you will get each block item
|
||||
# in rows of <li> tags.
|
||||
#
|
||||
# For instance:
|
||||
# list_of([['hello'], ['yall']]) { |i| i[0] }
|
||||
# or
|
||||
# list_of(['hello', 'yall'])
|
||||
#
|
||||
# Produces:
|
||||
# <li>hello</li>
|
||||
# <li>yall</li>
|
||||
#
|
||||
def list_of(array)
|
||||
(array.collect { |i| "<li>#{yield(i)}</li>" }).join("\n")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,6 +5,7 @@ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
|||
|
||||
require 'rubygems'
|
||||
require 'action_view'
|
||||
require 'active_support'
|
||||
|
||||
class HamlTest < Test::Unit::TestCase
|
||||
include Haml::Helpers
|
||||
|
@ -28,4 +29,9 @@ class HamlTest < Test::Unit::TestCase
|
|||
assert_equal(" ", tabs(1))
|
||||
assert_equal(" ", tabs(5))
|
||||
end
|
||||
|
||||
def test_list_of
|
||||
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}))
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue