simple tag-cloud Template Tag

 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
from django import template
import math

register = template.Library()

class Cloudify(template.Node):
	max_font = 40 # => Max fontsize in px
	min_font = 10 # => Min fontsize in px
	
	def __init__(self, tag_quantity, overall_quantity):
		self.tag_quantity = template.Variable(tag_quantity)
		self.overall_quantity = template.Variable(overall_quantity)
		
	def render(self, context):
		try:
			tag_amount = int(self.tag_quantity.resolve(context))
			overall_amount = int(self.overall_quantity.resolve(context))
                        try:
			    return "style=\"font-size: %ipx;\"" % int((Cloudify.max_font - Cloudify.min_font) * (math.log(tag_amount)) / (math.log(overall_amount)) + Cloudify.min_font)
                        except: # => for (math.log(0)) / (math.log(0)) or tag_amount == overall_amount
                            return ''
		except:
			return ''
		

@register.tag(name="cloudify")		
def do_cloudify(parser, token):
	try:
		tag_name, tag_quantity, overall_quantity = token.split_contents()
	except ValueError:
		raise template.TemplateSyntaxError, "%r tag requires two arguments" % token.contents.split()[0]
	else:
		return Cloudify(tag_quantity, overall_quantity)

More like this

  1. CSS Preprocessor by jokull 5 years, 7 months ago
  2. Cloud Tag template example by ramdas 5 years, 6 months ago
  3. Clouds Tag by ramdas 5 years, 6 months ago
  4. YUI Loader as Django middleware by akaihola 5 years, 1 month ago
  5. Clouds Tag by ramdas 5 years, 6 months ago

Comments

(Forgotten your password?)