git.fiddlerwoaroof.com
Raw Blame History
import xmlgen

class HTMLTemplate(object):
	def __init__(self):
		with xmlgen.Tag('html') as self.doc:
			with self.doc.child('head') as self.head:
				self.title = self.head.child('title')
			self.body = self.doc.child('body')

	def set_title(self, title):
		self.title.text(title)

	def add_script(self, src, type='text/javascript', async=False, defer=False):
		script = self.head.child('script', src=src, type=type)
		if async: script.attr('async', 'async')
		if defer: script.attr('defer', 'defer')
		return script

	def add_stylesheet(self, src, type='text/css', media=None):
		stylesheet = self.head.child('link', rel='stylesheet', type=type, href=src)
		if media is not None:
			stylesheet.attr('media', media)


	def __enter__(self, *_, **__):
		return self.body
	def __exit__(self, *_, **__):
		pass

	def __str__(self):
		return unicode(self.doc).encode('utf-8')