Skip to content

Instantly share code, notes, and snippets.

@smkelly
Last active December 12, 2015 12:48
Show Gist options
  • Select an option

  • Save smkelly/4774258 to your computer and use it in GitHub Desktop.

Select an option

Save smkelly/4774258 to your computer and use it in GitHub Desktop.

Revisions

  1. smkelly revised this gist Feb 12, 2013. 1 changed file with 16 additions and 16 deletions.
    32 changes: 16 additions & 16 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,23 @@
    def menu_link(text, target, icon=nil, attributes={})
    | # Find path
    # Find path
    path = target.is_a?(String) ? target : target.path
    raise RuntimeError, "Cannot create a link to #{target.inspect} because this target is not outputted (its routing rule returns nil)" if path.nil?
    raise RuntimeError, "Cannot create a link to #{target.inspect} because this target is not outputted (its routing rule returns nil)" if path.nil?

    # Check for current page
    if @item_rep && @item_rep.path == path
    activestr = " class='active'"
    else
    activestr = ""
    end
    # Check for current page
    if @item_rep && @item_rep.path == path
    activestr = " class='active'"
    else
    activestr = ""
    end

    # Handle the icon
    iconstr = icon ? "<i class='#{icon}'></i>" : ""
    # Handle the icon
    iconstr = icon ? "<i class='#{icon}'></i>" : ""

    # Join attributes
    attributes = attributes.inject('') do |memo, (key, value)|
    memo + key.to_s + '="' + h(value) + '" '
    end
    # Join attributes
    attributes = attributes.inject('') do |memo, (key, value)|
    memo + key.to_s + '="' + h(value) + '" '
    end

    # Create link
    "<li#{activestr}><a #{attributes}href='#{h path}'>#{iconstr}#{text}</a></li>"
    # Create link
    "<li#{activestr}><a #{attributes}href='#{h path}'>#{iconstr}#{text}</a></li>"
    end
  2. smkelly created this gist Feb 12, 2013.
    23 changes: 23 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    def menu_link(text, target, icon=nil, attributes={})
    | # Find path
    path = target.is_a?(String) ? target : target.path
    raise RuntimeError, "Cannot create a link to #{target.inspect} because this target is not outputted (its routing rule returns nil)" if path.nil?

    # Check for current page
    if @item_rep && @item_rep.path == path
    activestr = " class='active'"
    else
    activestr = ""
    end

    # Handle the icon
    iconstr = icon ? "<i class='#{icon}'></i>" : ""

    # Join attributes
    attributes = attributes.inject('') do |memo, (key, value)|
    memo + key.to_s + '="' + h(value) + '" '
    end

    # Create link
    "<li#{activestr}><a #{attributes}href='#{h path}'>#{iconstr}#{text}</a></li>"
    end