Stringtemplate Unicode Renderer

The stringtemplate library is a port of java template engine StringTemplate. The python stringtemplate documentation.

The default string rendering of the library doesn't handle unicode string. Below describes how I handle it.

import stringtemplate

class UnicodeStrRenderer(stringtemplate.AttributeRenderer):

def __init__(self):

pass

def str(self, o):

return o.encode('utf8')

dir = "tmp"

tgrp = stringtemplate.StringTemplateGroup('test', dir)

tgrp.registerRenderer(unicode, UnicodeStrRenderer())

There is a bug in StringTemplateGroup getAttributeRenderer.

def getAttributeRenderer(self, attributeClassType):

if not self.attributeRenderers:

if not self.superGroup:

return None # no renderers and no parent? Stop.

# no renderers; consult super group

return self.superGroup.getAttributeRenderer(attributeClassType)

if self.attributeRenderers.has_key(attributeClassType):

renderer = self.attributeRenderers[attributeClassType]

else:

# no renderer registered for this class, check super group

if not self.superGroup: return None # add this line to fix

renderer = self.superGroup.getAttributeRenderer(attributeClassType)

return renderer

Comments

blog comments powered by Disqus

Categories