Page 266 -
P. 266
template engine described
Let’s get to know the yate code before proceeding with the rest of this chapter. For each chunk
of code presented, you were to provide a written description of what you think it does:
from string import Template
Import the “Template” class from the standard library’s “string”
module. This allows for simple string-substitution templates.
Note the default
for “resp”.
def start_response(resp="text/html"):
return('Content-type: ' + resp + '\n\n')
This function takes a single (optional) string as its argument and uses it to
create a CGI “Content-type:” line, with “text/html” as the default.
Open the def include_header(the_title):
template file with open('templates/header.html') as headf:
(which is HTML), head_text = headf.read()
read it in, and
substitute in the header = Template(head_text)
provided “title”. return(header.substitute(title=the_title))
This function takes a single string as its argument and uses at the title for
the start of a HTML page. The page itself is stored within a separate file
in “templates/header.html”, and the title is substituted in as needed.
Open the template
file (which is
HTML), read it in, def include_footer(the_links):
with open('templates/footer.html') as footf:
and substitute in the
provided dictionary foot_text = footf.read()
of HTML links in link_string = ''
“the_links”. for key in the_links:
This looks a little
link_string += '<a href="' + the_links[key] + '">' + key + weird, but it’s an
'</a> '
HTML hack for
Turn the footer = Template(foot_text) forcing spaces
dictionary of return(footer.substitute(links=link_string)) into a string.
links into a string, Similar to the “include_header” function, this one uses its single string as
which is then its argument to create the end of a HTML page. The page itself is stored
substituted into
the template. within a separate file in “templates/footer.html”, and the argument is used
to dynamically create a set of HTML link tags. Based on how they are used,
it looks like the argument needs to be a dictionary.
230 Chapter 7