Module: Ramaze::Helper::XHTML
- Defined in:
- lib/ramaze/helper/xhtml.rb
Overview
Provides shortcuts to the link/script tags.
The XHTML helper can be used for generating CSS and Javascript tags. Generating a CSS tag can be done by calling the css() method:
css 'reset', 'screen', :only => 'ie'
This would result in a stylesheet named “reset.css” being loaded only when the user is using Internet Explorer.
Constant Summary collapse
- LINK_TAG =
'<link href=%p media=%p rel="stylesheet" type="text/css" />'
- SCRIPT_TAG =
'<script src=%p type="text/javascript"></script>'
Instance Method Summary collapse
-
#css(name, media = 'screen', options = {}) ⇒ String
Generate a CSS tag based on the name, media type and a hash containing additional options.
-
#css_for(*args) ⇒ String
The css_for method can be used when you want to load multiple stylesheets and don’t want to call the css() method over and over again.
-
#js(name, options = {}) ⇒ String
Generates a Javascript tag that loads an external Javascript file.
-
#js_for(*args) ⇒ String
Generate multiple Javascript tags using the js() method.
Instance Method Details
#css(name, media = 'screen', options = {}) ⇒ String
Generate a CSS tag based on the name, media type and a hash containing additional options. For example, if we want to load the stylesheet only when the user is using Internet Explorer we would have to add a key ‘only’ with a value of ‘ie’ to the hash.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/ramaze/helper/xhtml.rb', line 38 def css(name, media = 'screen', = {}) if media.respond_to?(:keys) = media media = 'screen' end if only = .delete(:only) and only.to_s == 'ie' "<!--[if IE]>#{css(name, media, )}<![endif]-->" else if name =~ /^http/ LINK_TAG % [name, media] else prefix = [:prefix] || 'css' LINK_TAG % [ "#{Ramaze..prefix.chomp("/")}/#{prefix}/#{name}.css", media ] end end end |
#css_for(*args) ⇒ String
The css_for method can be used when you want to load multiple stylesheets and don’t want to call the css() method over and over again.
77 78 79 |
# File 'lib/ramaze/helper/xhtml.rb', line 77 def css_for(*args) args.map{|arg| css(*arg) }.join("\n") end |
#js(name, options = {}) ⇒ String
Generates a Javascript tag that loads an external Javascript file. This tag can’t be used for loading inline Javascript files.
99 100 101 102 103 104 105 |
# File 'lib/ramaze/helper/xhtml.rb', line 99 def js(name, ={}) if name =~ /^http/ # consider it external full url SCRIPT_TAG % name else SCRIPT_TAG % "#{Ramaze..prefix.chomp("/")}/#{[:prefix] || 'js'}/#{name}.js" end end |
#js_for(*args) ⇒ String
Generate multiple Javascript tags using the js() method.
117 118 119 |
# File 'lib/ramaze/helper/xhtml.rb', line 117 def js_for(*args) args.map{|arg| js(*arg) }.join("\n") end |