- Author:
- agilitynerd
- Posted:
- September 26, 2009
- Language:
- Python
- Version:
- 1.1
- Score:
- 0 (after 0 ratings)
A basic templatetag to make it easier to insert ShrinkTheWeb images into templates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | """
Django template tag for inserting Shrink The Web images into templates.
Configuration:
Save this file as shrinktheweb_tags.py in the project's templatetags directory.
A variable named SHRINK_THE_WEB_KEY should be defined in settings.py set to the
user's STW key.
Usage:
{% load shrinktheweb_tags %}
{% shrinkthewebimage url size alt %}
where:
- url is expected to be a variable instantiated from the context
- size is passed directly to shrinktheweb.com which currently accepts
one of: "sm", "lg" or "xlg" (may be either a quoted string or not)
- alt is either a quoted string or a variable instantiated from the context
Example usage:
Given a template context variable "author" with an attribute "url"
the following are valid entries in a template file:
{% load shrinktheweb_tags %}
{% shrinkthewebimage author.url "sm" '' %}
{% shrinkthewebimage author.url lg author.url %}
{% shrinkthewebimage author.url 'xlg' "The author's website" %}
Copyright (c) 2009 Steve Schwarz
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"""
from django.utils.safestring import mark_safe
from django.conf import settings
from django import template
class FormatSTWImageNode(template.Node):
def __init__(self, url, size, alt):
self.args = dict(url=template.Variable(url),
size=size,
alt=alt,
key=settings.SHRINK_THE_WEB_KEY)
def render(self, context):
url = self.args['url'].resolve(context)
alt = self.args['alt']
if alt[0] == alt[-1] and alt[0] in ('"', "'"):
alt = alt[1:-1] # a string
else:
alt = template.Variable(alt).resolve(context)
size = self.args['size']
if size[0] == size[-1] and size[0] in ('"', "'"):
size = size[1:-1] # a string
key = self.args['key']
result = '''<img src="http://images.shrinktheweb.com/xino.php?stwembed=1stwaccesskeyid=%s&stwsize=%s&stwurl=%s" alt="%s"/>''' % (key, size, url, alt)
return result
def do_shrinkthewebimage(parser, token):
bits = token.split_contents()
if len(bits) != 4:
raise template.TemplateSyntaxError("'shrinkthewebimage' tag takes exactly 3 arguments")
return FormatSTWImageNode(url=bits[1], size=bits[2], alt=bits[3])
register = template.Library()
register.tag('shrinkthewebimage', do_shrinkthewebimage)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Corrected a bug where the tag couldn't be used within a loop.
#
Updated to match new STW web interface going into effect 7/2010
#
This snippet is out of date. Please use the official django-stw application
#
Please login first before commenting.