alecwh /

template engine
replaces values in a template with dict keys/values
$author: alecwh
$date: 08/08/2009

import glob
import os
from config import config
import web
import objects as db
import log

def templater(template_file, values):
	Opens the template and replaces all occurences of:
	%(key_name)s, etc
	with the corrosponding values in the provided dict
	# add default values for templates (in the config file)
	if type(values) == type(dict()):
		for x, y in config.general:
			values.update({x: y})
	if "site.html" in template_file:
		recent_comments_html = ''
		recent_comments =
		# 6 recent comments...
		for x in recent_comments[:3]:
			recent_comments_html += '<li><strong>%s</strong> posted %s</li>' % (, x.text[0:20]+u'...')
		# add recent comments list
		# render the template
		template = open(template_file).read()
		render = web.template.Template(template)
		return str(render(values))
	except KeyError:
		return "Key error"

if __name__ == "__main__":
	print "Testing the template engine..."
	print templater('templates/test.html')