Section 13.3. Bringing It All Together


13.3. Bringing It All Together

So, let's take a look at the master.kid file from a freshly created project. This contains a single match template that displays login information, Flash messages, and the TurboGears logo.

Chances are you'll want to keep something like this to have Flash and Identity messages automatically appear on your pages, but you'll likely want to edit the CSS references and add additional match templates:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ TR/xhtml1/DTD/xhtml1-transitional.dtd"> <?python import sitetemplate ?> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://purl.org/kid/ns#" py: extends="sitetemplate"> <head py:match="item.tag=='{http://www.w3.org/1999/xhtml}head'" py:attrs="item. items()">     <meta content="text/html; charset=UTF-8" http-equiv="content-type" py:replace="''"/>     <title py:replace="''">Your title goes here</title>     <meta py:replace="item[:]"/>     <style type="text/css">         #pageLogin         {             font-size: 10px;             font-family: verdana;             text-align: right;         }     </style> </head> <body py:match="item.tag=='{http://www.w3.org/1999/xhtml}body'" py:attrs="item. items()">     <div py:if="tg.config('identity.on',False) and not 'logging_in' in locals()"         >         <span py:if="tg.identity.anonymous">             <a href="/login">Login</a>         </span>         <span py:if="not tg.identity.anonymous">             Welcome ${tg.identity.user.display_name}.             <a href="/logout">Logout</a>         </span>     </div>     <div py:if="tg_flash"  py:content="tg_flash"></div>     <div py:replace="[item.text]+item[:]"/>     <p align="center"><img src="/books/4/370/1/html/2//static/images/tg_under_the_hood.png" alt="TurboGears under the hood"/></p> </body> </html>


Here are a few things you might want to look for in the above code:

  1. This py:match just provides sample headers, but ultimately passes the original headers through to the results of the match template untouched.

  2. py:attrs="item.items() takes all the item attributes from the element object and uses Kid's py:attrs directive to put them into the template output.

  3. The py:match expression matches the body element of all the pages that extend master.kid.

  4. Adds a login link to the top of the page.

  5. Display's login name if the user is logged in.

  6. Displays the Flash content.

  7. Puts the original body content back into the page.

  8. Adds a "powered by TurboGears" logo to the bottom of the page.

The master.kid page is written so that it could be opened directly in your browser or image-editing tool without the need to preprocess it with Kid. This way, designers have something they can work with on the "master" template.

The first py:match element matches the header of the incoming page. But if you look inside it, all the tags use py:replace to replace themselves with empty strings. This is what makes it possible to view this page directly in a browser. And at the end, we have a py:replace=item[:] that takes all the head subelements from the original page, and puts them back into the match template's output page.

With a basic understanding of what is happening in this page, you should be able to modify it to add headers, footers, sidebars, or just about anything you need on every page of your TurboGears project.




Rapid Web Applications with TurboGears(c) Using Python to Create Ajax-Powered Sites
Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites
ISBN: 0132433885
EAN: 2147483647
Year: 2006
Pages: 202

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net