Login

simple tag-cloud Template Tag

Author:
metty
Posted:
September 23, 2009
Language:
Python
Version:
1.1
Tags:
tag cloud logarithmic
Score:
1 (after 1 ratings)

This Template Tag computes the font-size of two given arguments and returns a CSS-encoded string like "font-size: XXpx;", which can be used to format the font-size of link. (The minium font-size must be set with CSS.)

Requires two arguments: 1. occurrence of the current tag 2. sum of all occurrences

It works great for my tag-cloud.

Source of the logarithmic formula

Usage

<a href="http://www.anything.com" {% cloudify variable.occurrence overall. occurrence_sum %} title="anything">any tag</a>

 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. Cloud Tag template example by ramdas 7 years, 4 months ago
  2. YUI Loader as Django middleware by akaihola 6 years, 11 months ago
  3. Clouds Tag by ramdas 7 years, 4 months ago
  4. Clouds Tag by ramdas 7 years, 4 months ago
  5. simple text image view by derelm 8 years ago

Comments

Please login first before commenting.