pythoncheetah

How to render unicode characters in a Cheetah template?


I would like to render a variable with Unicode characters, using the Cheetah template engine.

My template file template.txt looks like this:

This is static text in the template: äöü
This is filled by Cheetah: $variable

My program loads that file, and inserts a variable variable:

from Cheetah.Template import Template

data = [{"variable" : "äöü"}]

# open template
templateFile = open('template.txt', 'r')
templateString = templateFile.read()
templateFile.close()

template = Template(templateString, data)
filledText = str(template)

# Write filled template
filledFile = open('rendered.txt', 'w')
filledFile.write(filledText)
filledFile.close()

This creates a file in which the static Unicode characters are fine, but the dynamic ones are replaced by replacement characters.

This is static text in the template: äöü
This is filled by Cheetah: ���

All files are UTF-8, in case that matters.

How can I ensure that the characters are generated correctly?


Solution

  • Make all strings unicode, including strings from the file:

    data = [{"variable" : u"äöü"}]
    
    templateFile = codecs.open('template.txt', 'r', encoding='utf-8')
    
    filledFile = codecs.open('rendered.txt', 'w', encoding='utf-8')
    

    Get the result using unicode(), not str().

    This is not required but recommended — add #encoding utf-8 to the template:

    #encoding utf-8
    This is static text in the template: äöü
    This is filled by Cheetah: $variable
    

    See examples in Cheetah tests: https://github.com/CheetahTemplate3/cheetah3/blob/master/Cheetah/Tests/Unicode.py.