Hi Ksenia
passing variables to the template, and avoiding UnicodeError

<?xml version="1.0" encoding='iso-8859-1'?>
<?python
title="àòèìù"
title=unicode(title,'utf8')
para="Kid è buono e bravo."
para=unicode(para,'utf8')
?>
<html xmlns="http://www.w3.org/1999/xhtml";
      xmlns:py="http://purl.org/kid/ns#";>
<head>
        <title>${title}</title>
</head>
<body>
        <p>${para}</p>
</body>
</html>
So I have such template named tmp.
Now I try to serialize
If I'm going to serialize
>>> print tmp.serialize()
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml";>
<head>
        <title>à òèìù</title>
</head>
<body>
        <p>Kid è buono e bravo.</p>
</body>
</html>
>>>
as you see it don't work
But if I declare the encoding it works at well
print tmp.serialize(encoding='iso-8859-1')
<?xml version="1.0" encoding="iso-8859-1"?>
<html xmlns="http://www.w3.org/1999/xhtml";>
<head>
        <title>àòèìù</title>
</head>
<body>
        <p>Kid è buono e bravo.</p>
</body>
</html>
The better is
>>> from kid import XMLSerializer
>>> from kid.serialization import doctypes
>>> serial=XMLSerializer()
>>> serial.encoding='iso-8859-1'
>>> serial.decl=1
>>> serial.doctype=doctypes['xhtml-strict']
>>> print tmp.serialize(output=serial)
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml";>
<head>
        <title>àòèìù</title>
</head>
<body>
        <p>Kid è buono e bravo.</p>
</body>
</html>
>>> 
Is it ok?

Reply via email to