Module: Padrino::Helpers::AssetTagHelpers
- Defined in:
- lib/padrino-helpers/asset_tag_helpers.rb
Overview
Helpers related to producing assets (images, stylesheets, js, etc) within templates.
Constant Summary collapse
- APPEND_ASSET_EXTENSIONS =
["js", "css"]
- ABSOLUTE_URL_PATTERN =
%r{^(https?://)}
- ASSET_FOLDERS =
{ :js => 'javascripts', :css => 'stylesheets', }
Instance Method Summary collapse
-
#asset_path(kind, source = nil) ⇒ String
Returns the path to the specified asset (css or javascript).
-
#favicon_tag(source, options = {}) ⇒ String
Generates a favicon link.
-
#feed_tag(mime, url, options = {}) ⇒ String
Creates a link tag that browsers and news readers can use to auto-detect an RSS or ATOM feed.
-
#flash_tag(*args) ⇒ String
Creates a div to display the flash of given type if it exists.
-
#image_alt(src) ⇒ String
Returns a string suitable for an alt attribute of img element.
-
#image_path(src) ⇒ String
Returns the path to the image, either relative or absolute.
-
#image_tag(url, options = {}) ⇒ String
Creates an image element with given url and options.
-
#javascript_include_tag(*sources, options = {}) ⇒ String
Returns a html script tag for each of the sources provided.
-
#link_to(*args, &block) ⇒ String
Creates a link element with given name, url and options.
-
#mail_to(email, caption = nil, mail_options = {}) ⇒ String
Creates a mail link element with given name and caption.
-
#meta_tag(content, options = {}) ⇒ String
Creates a meta element with the content and given options.
-
#stylesheet_link_tag(*sources, options = {}) ⇒ String
Returns a html link tag for each of the sources provided.
Instance Method Details
#asset_path(kind, source = nil) ⇒ String
Returns the path to the specified asset (css or javascript).
316 317 318 319 320 321 322 323 324 |
# File 'lib/padrino-helpers/asset_tag_helpers.rb', line 316 def asset_path(kind, source = nil) kind, source = source, kind if source.nil? source = asset_normalize_extension(kind, escape_link(source.to_s)) return source if source =~ ABSOLUTE_URL_PATTERN || source =~ /^\// source = File.join(asset_folder_name(kind), source) = (source) result_path = uri_root_path(source) "#{result_path}#{}" end |
#favicon_tag(source, options = {}) ⇒ String
Generates a favicon link. Looks inside images folder
191 192 193 194 195 |
# File 'lib/padrino-helpers/asset_tag_helpers.rb', line 191 def favicon_tag(source, ={}) type = File.extname(source).sub('.','') = { :href => image_path(source), :rel => 'icon', :type => "image/#{type}" }.update() tag(:link, ) end |
#feed_tag(mime, url, options = {}) ⇒ String
Creates a link tag that browsers and news readers can use to auto-detect an RSS or ATOM feed.
@param options
The options for the feed tag.
117 118 119 120 |
# File 'lib/padrino-helpers/asset_tag_helpers.rb', line 117 def feed_tag(mime, url, ={}) full_mime = (mime == :atom) ? 'application/atom+xml' : 'application/rss+xml' tag(:link, { :rel => 'alternate', :type => full_mime, :title => mime, :href => url }.update()) end |
#flash_tag(*args) ⇒ String
Creates a div to display the flash of given type if it exists.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/padrino-helpers/asset_tag_helpers.rb', line 32 def flash_tag(*args) = args.last.is_a?(Hash) ? args.pop : {} bootstrap = .delete(:bootstrap) if [:bootstrap] args.inject(SafeBuffer.new) do |html,kind| next html unless flash[kind] flash_text = SafeBuffer.new << flash[kind] flash_text << content_tag(:button, '×'.html_safe, {:type => :button, :class => :close, :'data-dismiss' => :alert}) if bootstrap html << content_tag(:div, flash_text, { :class => kind }.update()) end end |
#image_alt(src) ⇒ String
Returns a string suitable for an alt attribute of img element.
223 224 225 |
# File 'lib/padrino-helpers/asset_tag_helpers.rb', line 223 def image_alt(src) File.basename(src, '.*').sub(/-[[:xdigit:]]{32,64}\z/, '').tr('-_', ' ').capitalize end |
#image_path(src) ⇒ String
Returns the path to the image, either relative or absolute. We search it in your appname.public_folder
like app/public/images for inclusion. You can provide also a full path.
289 290 291 |
# File 'lib/padrino-helpers/asset_tag_helpers.rb', line 289 def image_path(src) asset_path(:images, src) end |
#image_tag(url, options = {}) ⇒ String
Creates an image element with given url and options.
210 211 212 213 214 |
# File 'lib/padrino-helpers/asset_tag_helpers.rb', line 210 def image_tag(url, ={}) = { :src => image_path(url) }.update() [:alt] ||= image_alt(url) unless url.to_s =~ /\A(?:cid|data):|\A\Z/ tag(:img, ) end |
#javascript_include_tag(*sources, options = {}) ⇒ String
Returns a html script tag for each of the sources provided. You can pass in the filename without extension or a symbol and we search it in your appname.public_folder
like app/public/javascript for inclusion. You can provide also a full path.
266 267 268 269 270 271 272 273 |
# File 'lib/padrino-helpers/asset_tag_helpers.rb', line 266 def javascript_include_tag(*sources) = { :type => 'text/javascript' }.update(sources.last.is_a?(Hash) ? Utils.symbolize_keys(sources.pop) : {}) sources.flatten.inject(SafeBuffer.new) do |all,source| all << content_tag(:script, nil, { :src => asset_path(:js, source) }.update()) end end |
#link_to(caption, url, options = {}) ⇒ String #link_to(url, options = {}, &block) ⇒ String
Creates a link element with given name, url and options.
Note that you can pass :if
or :unless
conditions, but if you provide :current as condition padrino return true/false if the request.path_info match the given url.
84 85 86 87 88 89 90 91 |
# File 'lib/padrino-helpers/asset_tag_helpers.rb', line 84 def link_to(*args, &block) = args.last.is_a?(Hash) ? args.pop : {} name = block_given? ? '' : args.shift href = args.first = { :href => href ? escape_link(href) : '#' }.update() return name unless parse_conditions(href, ) block_given? ? content_tag(:a, , &block) : content_tag(:a, name, ) end |
#mail_to(email, caption = nil, mail_options = {}) ⇒ String
Creates a mail link element with given name and caption.
145 146 147 148 149 150 151 |
# File 'lib/padrino-helpers/asset_tag_helpers.rb', line 145 def mail_to(email, =nil, ={}) , = .partition{ |key,_| [:cc, :bcc, :subject, :body].include?(key) } mail_query = Rack::Utils.build_query(Hash[]).gsub(/\+/, '%20').gsub('%40', '@') mail_href = "mailto:#{email}" mail_href << "?#{mail_query}" unless mail_query.empty? link_to(( || email), mail_href, Hash[]) end |
#meta_tag(content, options = {}) ⇒ String
Creates a meta element with the content and given options.
170 171 172 173 |
# File 'lib/padrino-helpers/asset_tag_helpers.rb', line 170 def (content, ={}) = { "content" => content }.update() tag(:meta, ) end |
#stylesheet_link_tag(*sources, options = {}) ⇒ String
Returns a html link tag for each of the sources provided. You can pass in the filename without extension or a symbol and we search it in your appname.public_folder
like app/public/stylesheets for inclusion. You can provide also a full path.
242 243 244 245 246 247 248 249 250 |
# File 'lib/padrino-helpers/asset_tag_helpers.rb', line 242 def stylesheet_link_tag(*sources) = { :rel => 'stylesheet', :type => 'text/css' }.update(sources.last.is_a?(Hash) ? Utils.symbolize_keys(sources.pop) : {}) sources.flatten.inject(SafeBuffer.new) do |all,source| all << tag(:link, { :href => asset_path(:css, source) }.update()) end end |