aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/tag_helper.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2006-10-23 22:57:59 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2006-10-23 22:57:59 +0000
commit6c062054cdb236fead5d09679985ebb70d8b053a (patch)
treefc50fc2b66a928c89f1e009b060952d459546a58 /actionpack/lib/action_view/helpers/tag_helper.rb
parent9c9443812f1274667cea14edd387c6124205ac1c (diff)
downloadrails-6c062054cdb236fead5d09679985ebb70d8b053a.tar.gz
rails-6c062054cdb236fead5d09679985ebb70d8b053a.tar.bz2
rails-6c062054cdb236fead5d09679985ebb70d8b053a.zip
Added block-usage to TagHelper#content_tag [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5344 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view/helpers/tag_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/tag_helper.rb28
1 files changed, 24 insertions, 4 deletions
diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb
index f913c99abb..8b8c7b491c 100644
--- a/actionpack/lib/action_view/helpers/tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/tag_helper.rb
@@ -15,11 +15,26 @@ module ActionView
end
# Examples:
- # * <tt>content_tag("p", "Hello world!") => <p>Hello world!</p></tt>
- # * <tt>content_tag("div", content_tag("p", "Hello world!"), "class" => "strong") => </tt>
+ # * <tt>content_tag(:p, "Hello world!") => <p>Hello world!</p></tt>
+ # * <tt>content_tag(:div, content_tag(:p, "Hello world!"), :class => "strong") => </tt>
# <tt><div class="strong"><p>Hello world!</p></div></tt>
- def content_tag(name, content, options = nil)
- "<#{name}#{tag_options(options.stringify_keys) if options}>#{content}</#{name}>"
+ #
+ # ERb example:
+ # <% content_tag :div, :class => "strong" %>
+ # Hello world!
+ # <% end %>
+ #
+ # Will output:
+ # <div class="strong"><p>Hello world!</p></div>
+ def content_tag(name, content_or_options_with_block = nil, options = nil, &block)
+ if block_given?
+ options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
+ content = capture(&block)
+ concat(content_tag_string(name, content, options), block.binding)
+ else
+ content = content_or_options_with_block
+ content_tag_string(name, content, options)
+ end
end
# Returns a CDATA section for the given +content+. CDATA sections
@@ -41,6 +56,11 @@ module ActionView
end
private
+ def content_tag_string(name, content, options)
+ tag_options = options ? tag_options(options.stringify_keys) : ""
+ "<#{name}#{tag_options}>#{content}</#{name}>"
+ end
+
def tag_options(options)
cleaned_options = convert_booleans(options.stringify_keys.reject {|key, value| value.nil?})
' ' + cleaned_options.map {|key, value| %(#{key}="#{escape_once(value)}")}.sort * ' ' unless cleaned_options.empty?