diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2004-11-24 01:04:44 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2004-11-24 01:04:44 +0000 |
commit | db045dbbf60b53dbe013ef25554fd013baf88134 (patch) | |
tree | 257830e3c76458c8ff3d1329de83f32b23926028 /actionpack/lib/action_view/helpers/tag_helper.rb | |
download | rails-db045dbbf60b53dbe013ef25554fd013baf88134.tar.gz rails-db045dbbf60b53dbe013ef25554fd013baf88134.tar.bz2 rails-db045dbbf60b53dbe013ef25554fd013baf88134.zip |
Initial
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4 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.rb | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb new file mode 100644 index 0000000000..90084c7a8d --- /dev/null +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -0,0 +1,59 @@ +require 'cgi' + +module ActionView + module Helpers + # This is poor man's Builder for the rare cases where you need to programmatically make tags but can't use Builder. + module TagHelper + include ERB::Util + + # Examples: + # * tag("br") => <br /> + # * tag("input", { "type" => "text"}) => <input type="text" /> + def tag(name, options = {}, open = false) + "<#{name + tag_options(options)}" + (open ? ">" : " />") + end + + # Examples: + # * content_tag("p", "Hello world!") => <p>Hello world!</p> + # * content_tag("div", content_tag("p", "Hello world!"), "class" => "strong") => + # <div class="strong"><p>Hello world!</p></div> + def content_tag(name, content, options = {}) + "<#{name + tag_options(options)}>#{content}</#{name}>" + end + + # Starts a form tag that points the action to an url configured with <tt>url_for_options</tt> just like + # ActionController::Base#url_for. + def form_tag(url_for_options, options = {}, *parameters_for_url) + html_options = { "method" => "POST" }.merge(options) + + if html_options[:multipart] + html_options["enctype"] = "multipart/form-data" + html_options.delete(:multipart) + end + + html_options["action"] = url_for(url_for_options, *parameters_for_url) + + tag("form", html_options, true) + end + + alias_method :start_form_tag, :form_tag + + # Outputs "</form>" + def end_form_tag + "</form>" + end + + + private + def tag_options(options) + if options.empty? + "" + else + " " + options.collect { |pair| + "#{pair.first}=\"#{html_escape(pair.last)}\"" + }.sort.join(" ") + end + end + end + end +end
\ No newline at end of file |