Prepare accented characters for HTML

Tony van der Hoff lists at vanderhoff.org
Thu Mar 28 13:09:21 EDT 2019


On 28/03/2019 16:58, Chris Angelico wrote:
> On Fri, Mar 29, 2019 at 3:47 AM Tony van der Hoff <lists at vanderhoff.org> wrote:
>>
>> On 28/03/2019 15:09, Peter Otten wrote:
>>> Tony van der Hoff wrote:
>>>
>>>> On 28/03/2019 12:46, Jon Ribbens wrote:
>>>>> On 2019-03-28, Tony van der Hoff <lists at vanderhoff.org> wrote:
>>>>>> Thanks, Chris. The problem is not with the browser, but Jinja crashes.
>>>>>> Probably a bug, but I'm too wedded to that engine to change now. I'll
>>>>>> raise it on the Jinja bug site.
>>>>>
>>>>> It'll almost certainly be a mistake in the way you're using Jinja.
>>>>> I can't believe nobody's used non-ASCII characters in Jinja before.
>>>>>
>>>>
>>>> I'm open to suggestions.
>>>
>>> You have to describe the "crash". If you can provide a small script to
>>> reproduce it that would be best. For demonstration purposes feed the
>>> renderer a constant string instead of reading from the db.
>>>
>>> You should also tell us which version of Python and Jinja you are using.
>>>
>>>
>> OK,The crash is evidenced by an empty web page being generated,
>> containing just <html><head></head><body></body></html>
>> elements, with no content. No error messages nor exceptions.
>>
>> I.m using python3.5.3 and jinja 2.10.
>>
>> I have placed a sample script with a jnj template at
>> https://drive.google.com/drive/folders/1rM5F46wRqHYn0VBXUhSl8DkNcwsp2u8b?usp=sharing
>>
>> The template contains a commented-out line, which when uncommented shows
>> the alleged bug.
> 
> I can't see any of the code. Are you able to share it in a more
> code-friendly way, such as linking to a repository on GitHub, GitLab,
> BitBucket, SourceForge, or something else (probably with a capital
> letter in the middle of the name, for consistency)?
> 
> Or, even better: create a short enough example that you can just
> include it in the body of your post?
> 

I hate Google!

This'll probably work:

accent-test/accent-test.py:
#####################################################################
#!/usr/bin/env python3

import os
from jinja2 import Environment, FileSystemLoader

PATH = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_ENVIRONMENT = Environment(
    autoescape=False,
    loader=FileSystemLoader(os.path.join(PATH, 'templates')),
    trim_blocks=False)


def render_template(template_filename, context):
    return
TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)


def create_index_html():

    # put the list into a dictionary for rendering
    context = {
                'title': "accent-test",
                'french': 'année',
                'french1': 'année',
              }

    # render the template to html
    print ("Content-type: text/html\n\n")
    print (render_template('accent-test.jnj', context))

def main():
    create_index_html()

########################################

if __name__ == "__main__":
    main()
#####################################################################

accent-test/templates/accent-test.jnj:

#####################################################################
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
    <title>{{title}}</title>
  </head>
  <body>
    <center>
      <h1>{{title}}</h1>
{#
      <p>{{french}}</p>
#}
      <p>{{french1}}</p>
    </center>
  </body>
</html>

#####################################################################

-- 
Tony van der Hoff        | mailto:tony at vanderhoff.org
Buckinghamshire, England |



More information about the Python-list mailing list