Login

html helpers for images and links

Author:
buriy
Posted:
October 9, 2007
Language:
Python
Version:
.96
Tags:
construction a-href img-src html helper link image template
Score:
6 (after 6 ratings)

link and img are both HTML construction helpers. Good idea to use these helpers if your html don't fit into templates.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def link(url, text='', classes=''):
    if not text: text = url
    if classes: classes = ' class="%s"' % classes
    return """<a href="%s"%s>%s</a>""" % (url, classes, text)

def img(url, alt='', classes=''):
    if not url.startswith('http://') and not url[:1]=='/':
        #add media_url for relative paths
        import settings
        url = settings.MEDIA_URL + url
    if classes: classes = ' class="%s"' % classes
    if alt: alt = ' alt="%s" title="%s"' % (alt, alt)
    return """<img src="%s"%s%s>""" % (url, classes, alt)

More like this

  1. Simple "html email with images" sender by andres_torres_marroquin 4 years, 4 months ago
  2. iPernity thumbnail helper by sedden 6 years, 7 months ago
  3. Auto Generate/Save Thumbnails using Template Filter (scale max_x, max_y, or both) by ThisbeTom 6 years, 6 months ago
  4. Dom ID by bmihelac 5 years, 7 months ago
  5. Sending html emails with images using Django templates by sleytr 8 years ago

Comments

Please login first before commenting.