O'Reilly logo

Using Google App Engine by Charles Severance

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Conditional Processing Templates

Our application has several pages, and although we have moved most of the repeated text into a base file, there is one area in the _base.htm that needs to change between files. If we look at the pages, we see that as we move between pages, we want to have the navigation links colored differently to indicate which page we are currently looking at, as shown in Figure 6-8.

Navigation details

Figure 6-8. Navigation details

We make this change by using the selected class in the generated HTML. For example on the topics.htm file, we need the “Topics” link to be indicated as “selected”:

<ul>
   <li><a href="sites.htm">Sites</a></li>
   <li><a href="topics.htm" class="selected">Topics</a></li>
 </ul>

We need to generate this text differently on each page, and the generated text depends on the file we are rendering. The path indicates which page we are on, so when we are on the Topics page, the path is /topics.htm.

We make a small change to the handler to pass the current path into the render process as follows:

class MainHandler(webapp.RequestHandler):

  def get(self):
    path = self.request.path
    temp = os.path.join(
        os.path.dirname(__file__),
        'templates/' + path)
    if not os.path.isfile(temp):
        temp = os.path.join(
            os.path.dirname(__file__),
            'templates/index.htm')

    outstr = template.render(temp, { 'path': path })
    self.response.out.write(outstr)

Instead of passing an empty dictionary as the second ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required