Using Python CGI for a big HTML file
Question: I have a big html file named exercise.html, I need generate to one som stuff
by Python CGI. I want to ask you what is the best way to print this HTML.

I know that it is possible by print method with using format methods %s, %i
etc.:

    
    
    print '''<html>
    <head><title>My first Python CGI app</title></head>
    <body> 
    <p>Hello, 'world'!</p>
    .
    .
    <div>%s</div>
    .
    .
    </body>
    </html>''' % generated_text
    

But this HTML is really big,so is this only one solution?


Answer: You should consider using a templating language like
[Jinja2](http://jinja.pocoo.org/docs/intro/#basic-api-usage).

Here is a simple example straight from the link above:

    
    
    >>> from jinja2 import Template
    >>> template = Template('Hello {{ name }}!')
    >>> template.render(name='John Doe')
    

Generally, though you save templates in a file, and then load / process them:

    
    
    from jinja2 import Environment, PackageLoader
    # The env object below finds templates that are lcated in the `templates`
    # directory of your `yourapplication` package.
    env = Environment(loader=PackageLoader('yourapplication', 'templates'))
    template = env.get_template('mytemplate.html')
    print template.render(the='variables', go='here')
    

As demonstrated above, templates let you put `variables` into the template.
Placing text inside {{ }} makes it a template variable. When you render the
template, pass in the variable value with a keyword argument. For instance,
the template below has a name variable that we pass via template.render

> This is my {{name}}.
    
    
    template.render(name='Jaime')
    

