Pylons

Apr 10, 2009 8:19:30 PM

Create a link to a image or to a link passed as argument

This function is called after link_to which does the same thing except that instead of a label, it render a link with an image literal.
def image_to(src, alt, url=None, width=None, height=None, **attrs):
    src = url_for(src)

    if url:
        url = url_for(url)
    else:
        url = src

    return link_to(image(src, alt, width, height, **attrs), url)

Comments (44)

Loïc Faure-Lacroix
Apr 10, 2009 8:28:15 PM

I forgot to write how tu use the function. I guess it’s pretty simple

With image source and alt attribute:
h.image_to(’/images/something.png’, ‘Something’)
>> literal(u’
Something’)

With image source, alt attribute and link url
h.image_to(’/images/something.png’, ‘Something’, ‘test.html’)
>> literal(u’Something’)

Same thing as above with url_for
h.image_to(’/images/something.png’, ‘Something’, h.url_for(controller=‘event’, action=‘view’, id=4))
>> literal(u’Something’)

I don’t know if there is a better way to do it like, I guess the url_for in my snippet is useless.

Loïc Faure-Lacroix
Apr 10, 2009 8:35:24 PM

def image_to(src, alt, url=None, width=None, height=None, **attrs):
if not url:
url = src

return link_to(image(src, alt, width, height, **attrs), url)

You must login before you can comment.

Powered by Pylons - Contact Administrators