Last active
December 12, 2015 12:48
-
-
Save smkelly/4774258 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment