0

I'm building a simple hierarchical template with jinja. I was splitting my monolithic html file in smaller chunks to reuse them. In my html I had accented characters like èàù... when I tried to run the app I received errors complaining about my files not beeing properly encoded. So I decided to adhere to utf-8 standars and substituted those characters with things like ù and so on... This worked but is seems a bit cumbersome as a workflow. I'm wandering which is the correct approach to this kind of problems both with template files and content that have to be rendered in those templates.

I suppose that it is good practice to encode texts in utf-8 before storing them but I'm not sure.

JackNova
  • 3,911
  • 5
  • 31
  • 49

2 Answers2

1

When you use utf-8 in your (html) files, templates and python code it works fine and you can use : èàù...

Make sure your editors use utf-8 encoded files are read this article about utf-8. : http://blog.notdot.net/2010/07/Getting-unicode-right-in-Python. You also can use :

#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

at the start of your Python modules.

voscausa
  • 11,253
  • 2
  • 39
  • 67
  • similar question here http://stackoverflow.com/questions/13807748/when-to-use-utf8-as-a-header-in-py-files/13807795#13807795 –  Jan 26 '13 at 13:20
0

In templates, it is sufficient to save the file with correct utf-8 encoding, see image below. For what concerns strings that you want to store in source code and pass around in your program, if you only need a quick and dirty suggestion, here it is:

  1. save your .py files as utf-8
  2. now you can use all characters you want in your strings
  3. you only need explicitly inform the rest of the system that you are using an utf-8 encode

enter image description here

EXAMPLE

my_string = """I write àny character I want herè, just only ensurè that the encoding for the file you are writing is utf-8 àèàééàà"""

def utf8(s):
    return unicode(s, 'utf8')

pass_string_around = utf8(my_string)
JackNova
  • 3,911
  • 5
  • 31
  • 49