Page 264 -
P. 264

template engine code







                               Let’s get to know the yate code before proceeding with the rest of this chapter. For each chunk
                               of code presented, provide a written description of what you think it does in the spaces provided:

                                                                                                    Write your
             T ake a moment to   from string import Template                                        explanations in
             look up the “T emplate”                                                                the spaces.
              module in Python’s
              documentation set.



                               def start_response(resp="text/html"):
                                   return('Content-type: ' + resp + '\n\n')
               One has already    This function takes a single (optional) string as its argument and uses it to
               been done for you.
                                   create a CGI “Content-type:” line, with “text/html” as the default.


                               def include_header(the_title):
                                   with open('templates/header.html') as headf:
                                       head_text = headf.read()
                                   header = Template(head_text)
                                   return(header.substitute(title=the_title))








                               def include_footer(the_links):
                                   with open('templates/footer.html') as footf:
                                       foot_text = footf.read()
                                   link_string = ''
                                   for key in the_links:
                                       link_string += '<a href="' + the_links[key] + '">' + key +
                                                           '</a>    '
                                   footer = Template(foot_text)
                                   return(footer.substitute(links=link_string))










           228    Chapter 7
   259   260   261   262   263   264   265   266   267   268   269