From 2a5fc27ceac142762dc66680364be17b100ab0b8 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 19 Feb 2005 16:04:50 +0000 Subject: 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 --- .../lib/action_view/helpers/asset_tag_helper.rb | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/asset_tag_helper.rb (limited to 'actionpack/lib') 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 :rss (default) or :atom and the +options+ follow the url_for style of declaring a link target. + # + # Examples: + # auto_discovery_link_tag # => + # + # auto_discovery_link_tag(:atom) # => + # + # auto_discovery_link_tag(:rss, :action => "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" # => + # + # + # javascript_include_tag "common.javascript", "/elsewhere/cools" # => + # + # + 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" # => + # + # + # stylesheet_link_tag "random.styles", "/css/stylish" # => + # + # + 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 -- cgit v1.2.3