Class: Crummy::StandardRenderer
- Inherits:
-
Object
- Object
- Crummy::StandardRenderer
- Includes:
- ActionView::Helpers::TagHelper, ActionView::Helpers::UrlHelper
- Defined in:
- lib/crummy/standard_renderer.rb
Instance Method Summary collapse
-
#render_crumbs(crumbs, options = {}) ⇒ Object
Render the list of crumbs as either html or xml.
Instance Method Details
#render_crumbs(crumbs, options = {}) ⇒ Object
Render the list of crumbs as either html or xml
Takes 3 options: The output format. Can either be xml or html. Default :html
:format => (:html|:xml)
The separator text. It does not assume you want spaces on either side so you must specify. Default » for :html and crumb
for xml
:separator => string
Render links in the output. Default true
:link => boolean
Examples:
render_crumbs #=> <a href="/">Home</a> » <a href="/businesses">Businesses</a>
render_crumbs :separator => ' | ' #=> <a href="/">Home</a> | <a href="/businesses">Businesses</a>
render_crumbs :format => :xml #=> <crumb href="/">Home</crumb><crumb href="/businesses">Businesses</crumb>
render_crumbs :format => :html_list #=> <ul class="" id=""><li class=""><a href="/">Home</a></li><li class=""><a href="/">Businesses</a></li></ul>
With :format => :html_list you can specify additional params: li_class, ul_class, ul_id The only argument is for the separator text. It does not assume you want spaces on either side so you must specify. Defaults to »
render_crumbs(" . ") #=> <a href="/">Home</a> . <a href="/businesses">Businesses</a>
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/crummy/standard_renderer.rb', line 30 def render_crumbs(crumbs, = {}) [:skip_if_blank] ||= Crummy.configuration.skip_if_blank return '' if [:skip_if_blank] && crumbs.count < 1 [:format] ||= Crummy.configuration.format [:separator] ||= Crummy.configuration.send(:"#{[:format]}_separator") [:right_separator] ||= Crummy.configuration.send(:"#{[:format]}_right_separator") [:links] ||= Crummy.configuration.links [:first_class] ||= Crummy.configuration.first_class [:last_class] ||= Crummy.configuration.last_class [:microdata] ||= Crummy.configuration.microdata if [:microdata].nil? [:truncate] ||= Crummy.configuration.truncate if [:truncate] [:last_crumb_linked] = Crummy.configuration.last_crumb_linked if [:last_crumb_linked].nil? [:right_side] ||= Crummy.configuration.right_side last_hash = lambda {|o|k=o.map{|c| c.is_a?(Hash) ? (c.empty? ? nil: c) : nil}.compact k.empty? ? {} : k.last } local_global = lambda {|crumb, , param_name| last_hash.call(crumb).has_key?(param_name.to_sym) ? last_hash.call(crumb)[param_name.to_sym] : [param_name.to_sym]} case [:format] when :html crumb_string = crumbs.map{|crumb|local_global.call(crumb, , :right_side) ? nil : crumb_to_html(crumb, local_global.call(crumb, , :links), local_global.call(crumb, , :first_class), local_global.call(crumb, , :last_class), (crumb == crumbs.first), (crumb == crumbs.last), local_global.call(crumb, , :microdata), local_global.call(crumb, , :last_crumb_linked), local_global.call(crumb, , :truncate))}.compact.join([:separator]).html_safe crumb_string when :html_list # Let's set values for special options of html_list format [:li_class] ||= Crummy.configuration.li_class [:ul_class] ||= Crummy.configuration.ul_class [:ul_id] ||= Crummy.configuration.ul_id [:ul_id] = nil if [:ul_id].blank? crumb_string = crumbs.map{|crumb|local_global.call(crumb, , :right_side) ? nil : crumb_to_html_list(crumb, local_global.call(crumb, , :links), local_global.call(crumb, , :li_class), local_global.call(crumb, , :first_class), local_global.call(crumb, , :last_class), (crumb == crumbs.first), (crumb == crumbs.find_all{|crumb| !last_hash.call(crumb).fetch(:right_side,false)}.compact.last), local_global.call(crumb, , :microdata), local_global.call(crumb, , :last_crumb_linked), local_global.call(crumb, , :truncate), local_global.call(crumb, , :separator))}.compact.join.html_safe crumb_right_string = crumbs.reverse.map{|crumb|!local_global.call(crumb, , :right_side) ? nil : crumb_to_html_list(crumb, local_global.call(crumb, , :links), local_global.call(crumb, , :li_right_class), local_global.call(crumb, , :first_class), local_global.call(crumb, , :last_class), (crumb == crumbs.first), (crumb == crumbs.find_all{|crumb|!local_global.call(crumb, , :right_side)}.compact.last), local_global.call(crumb, , :microdata), local_global.call(crumb, , :last_crumb_linked), local_global.call(crumb, , :truncate), local_global.call(crumb, , :right_separator))}.compact.join.html_safe crumb_string = content_tag(:ul, crumb_string+crumb_right_string, :class => [:ul_class], :id => [:ul_id]) crumb_string when :xml crumbs.collect do |crumb| crumb_to_xml(crumb, local_global.call(crumb, , :links), local_global.call(crumb, , :separator), (crumb == crumbs.first), (crumb == crumbs.last)) end * '' else raise ArgumentError, "Unknown breadcrumb output format" end end |