Created
March 13, 2011 14:42
-
-
Save Apkawa/868138 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
import os | |
from django import template | |
from django.utils.safestring import mark_safe | |
register = template.Library() | |
ICON_PATH = [os.path.join('icons', 'led-icons'), os.path.join('icons', 'clever_icons') ] | |
ALIASES = { | |
'add':'add-icon', | |
'edit':'pencil', | |
'delete':'cross', | |
'archive':'box', | |
} | |
def get_icon_url(icon_name): | |
icon_file = ALIASES.get( icon_name, icon_name) + ".png" | |
for icon_path in ICON_PATH: | |
test_path = os.path.join( settings.MEDIA_ROOT, icon_path, icon_file ) | |
if os.path.exists( test_path ): | |
icon_url = os.path.join(settings.MEDIA_URL, icon_path, icon_file ) | |
return icon_url | |
icon_url = '' | |
return icon_url | |
@register.simple_tag | |
def icon_img( icon_name, alt=None, href=False): | |
icon_url = get_icon_url(icon_name) | |
if href: | |
return icon_url | |
if not alt: | |
return mark_safe( "<img class=\"icon\" src=\"%s\" alt=\"%s\"/>"%( icon_url, icon_name) ) | |
else: | |
return mark_safe( "<img class=\"icon\" src=\"%s\" alt=\"%s\"/>"%( icon_url, alt) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment