aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/asset_tag_helper.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-02-19 16:04:50 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-02-19 16:04:50 +0000
commit2a5fc27ceac142762dc66680364be17b100ab0b8 (patch)
tree2358154541295547ab1c48003a288dad209d8fdc /actionpack/lib/action_view/helpers/asset_tag_helper.rb
parentad1fe7dd277e4e82bd8588e06c1181b6f8402683 (diff)
downloadrails-2a5fc27ceac142762dc66680364be17b100ab0b8.tar.gz
rails-2a5fc27ceac142762dc66680364be17b100ab0b8.tar.bz2
rails-2a5fc27ceac142762dc66680364be17b100ab0b8.zip
Added AssetTagHelper that provides methods for linking a HTML page together with other assets, such as javascripts, stylesheets, and feeds.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@689 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view/helpers/asset_tag_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/asset_tag_helper.rb59
1 files changed, 59 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
new file mode 100644
index 0000000000..33914b2144
--- /dev/null
+++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
@@ -0,0 +1,59 @@
+require 'cgi'
+require File.dirname(__FILE__) + '/url_helper'
+require File.dirname(__FILE__) + '/tag_helper'
+
+module ActionView
+ module Helpers
+ # Provides methods for linking a HTML page together with other assets, such as javascripts, stylesheets, and feeds.
+ module AssetTagHelper
+ # Returns a link tag that browsers and news readers can use to auto-detect a RSS or ATOM feed for this page. The +type+ can
+ # either be <tt>:rss</tt> (default) or <tt>:atom</tt> and the +options+ follow the url_for style of declaring a link target.
+ #
+ # Examples:
+ # auto_discovery_link_tag # =>
+ # <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.curenthost.com/controller/action" />
+ # auto_discovery_link_tag(:atom) # =>
+ # <link rel="alternate" type="application/atom+xml" title="ATOM" href="http://www.curenthost.com/controller/action" />
+ # auto_discovery_link_tag(:rss, :action => "feed") # =>
+ # <link rel="alternate" type="application/atom+xml" title="ATOM" href="http://www.curenthost.com/controller/feed" />
+ def auto_discovery_link_tag(type = :rss, options = {})
+ tag(
+ "link", "rel" => "alternate", "type" => "application/#{type}+xml", "title" => type.to_s.upcase,
+ "href" => url_for(options.merge(:only_path => false))
+ )
+ end
+
+ # Returns a script include tag per source given as argument. Examples:
+ #
+ # javascript_include_tag "xmlhr" # =>
+ # <script language="JavaScript" type="text/javascript" src="/javascripts/xmlhr.js"></script>
+ #
+ # javascript_include_tag "common.javascript", "/elsewhere/cools" # =>
+ # <script language="JavaScript" type="text/javascript" src="/javascripts/common.javascript"></script>
+ # <script language="JavaScript" type="text/javascript" src="/elsewhere/cools.js"></script>
+ def javascript_include_tag(*sources)
+ sources.collect { |source|
+ source = "/javascripts/#{source}" unless source.include?("/")
+ source = "#{source}.js" unless source.include?(".")
+ content_tag("script", "", "language" => "JavaScript", "type" => "text/javascript", "src" => source)
+ }.join("\n")
+ end
+
+ # Returns a css link tag per source given as argument. Examples:
+ #
+ # stylesheet_link_tag "style" # =>
+ # <link href="/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" />
+ #
+ # stylesheet_link_tag "random.styles", "/css/stylish" # =>
+ # <link href="/stylesheets/random.styles" media="screen" rel="Stylesheet" type="text/css" />
+ # <link href="/css/stylish.css" media="screen" rel="Stylesheet" type="text/css" />
+ def stylesheet_link_tag(*sources)
+ sources.collect { |source|
+ source = "/stylesheets/#{source}" unless source.include?("/")
+ source = "#{source}.css" unless source.include?(".")
+ tag("link", "rel" => "Stylesheet", "type" => "text/css", "media" => "screen", "href" => source)
+ }.join("\n")
+ end
+ end
+ end
+end