Section 3.5. Understanding Kid and TurboGears Views


3.5. Understanding Kid and TurboGears Views

TurboGears uses the Kid templating system to do all the formatting and display logic work that comprises the view section of the MVC paradigm. If you are used to PHP, Cheetah, JSP, or ASP, you'll quickly notice that Kid has its own way of doing things. But if you stick around long enough, you'll notice several important advantages to the Kid way.

First of all, if you work with a designer, she'll fall in love with you right away when you show her how she can open up Kid templates easily in Firefox or Dreamweaver.

On the other hand, if you're not lucky enough to work with a great designer, perhaps you'd like to use Dreamweaver, Nvu, or some other WYSIWYG tool to edit your designs yourself (in which case, you'll be the one falling in love with Kid).

I'm sure plenty of you are the hard-core programmers who write a half dozen well-formed 200KB XHTML+CSS and XML configuration files from memory before breakfast. Kid has a lot for you to love, too.

Basically, Kid templates are XML documents with special namespace attributes that tell the Kid processor how to process the template. Most of the time, this means your Kid templates will be valid XHTML documents before they are processed, and valid HTML documents after they are processed and sent out to the user's browser.

The fact that Kid can guarantee that your documents will be well formed has lots of benifits. For example, as discussed in Chapter 8, "RSS, Cookies, and Dynamic Views in Fast Track," the JavaScript function innerHTML() works great (as long as you give it valid HTML, and Kid output is always valid).

Of course, if you want to, you can use Kid to output all kinds of other XML documents, from XHTML to RSS feeds, XML-RPC, or whatever you happen to need.

But that's probably getting ahead of things. Right now, you just want to see some basic Kid templates so that you can get your mind around how this all works:

<?python some_text = "SomeText is in the title now!" ?> <html xmlns:py="http://purl.org/kid/ns#">   <head>     <title py:content="some_text">This is replaced.</title>   </head>   <body>     If you loaded this in Firefox directly the title is "This is Replaced" But     if you opened through TurboGears the title will be " SomeText is in the title now!"   </body> </html>


If you save this snippet of code as welcome.kid and throw it in the templates directory of a freshly quick-started project, you can open up the Kid file directly in your browser, or you can start up your server and see the brand new title.

The preceding example shows how you can use the <?python ?> processing instruction to embed pure Python code in your template. But beware, you are not allowed to return strings to the template from a <?python ?> processing instruction. This might seem like a pain at first, but it really helps to maintain the designer friendliness of Kid. Another even more important benefit of this restriction is that it helps you to avoid the kind of ugly spaghetti code mess of application logic mixed in with the markup code that you often see in PHP, ASP, or JSP code.

The example also shows how you use the py:content tag on an attribute to replace the content of that attribute with the value returned by some Python expression. This is one of the keys to making your templates work well in Nvu and Dreamweaver.

If you want to take a shortcut, you can replace the <title> attribute described previously with the following:

<title>${SomeText}</title>


This will have the same result when you run it from within TurboGears. But if you open it in Firefox, Nvu, or Dreamweaver, the title will just be ${SomeText}, which isn't all that pretty, and because it's not hidden in an attribute, it is much easier for a designer to accidentally change, and therefore break, your application. Nobody wants that!




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

Similar book on Amazon

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