Blame | Last modification | View Log | RSS feed
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"xmlns:py="http://genshi.edgewall.org/"xmlns:xi="http://www.w3.org/2001/XInclude"><xi:include href="master.html" /><head><meta content="text/html; charset=UTF-8" http-equiv="content-type"py:replace="''" /><title>Learning TurboGears 2.0: Quick guide to the Quickstartpages.</title></head><body>${sidebar_top()} ${sidebar_bottom()}<div id="getting_started"><h2>Architectural basics of a quickstart TG2 site with some vars${val1} and ${val2}.</h2><p>The TG2 quickstart command produces this basic TG site. Here'show it works.</p><ol id="getting_started_steps"><li class="getting_started"><h3>Code my data model</h3><p>When you want a model for storing favorite links or wikicontent, the <strong>/model</strong> folder in your site is ready togo.</p><p>You can build a dynamic site without any data model at all.There still be a default data-model template for you if you didn'tenable authentication and authorization in quickstart. If you enabledit, you got auth data-model made for you.</p></li><li class="getting_started"><h3>Design my URL structure</h3><p>The "<span class="code">root.py</span>" file under the <strong>/controllers</strong>folder has your URLs. When you called this url (<span class="code"><ahref="${tg.url('/about')}">about</a></span>), the command went through theRootController class to the <span class="code">about</span><spanclass="code">()</span> method.</p><p>Those Python methods are responsible to create the dictionary ofvariables that will be used in your web views (template).</p></li><li class="getting_started"><h3>Reuse the web page elements</h3><p>A web page viewed by user could be constructed by single orseveral reusable templates under <strong>/templates</strong>. Take'about' page for example, each reusable templates generating a part ofthe page. We'll cover them in the order of where they are found, listednear the top of the about.html template</p><p><strong><span class="code">header.html</span></strong> - The"header.html" template contains the HTML code to display the 'header':The blue gradient, TG2 logo, and some site text at the top of everypage it is included on. When the "about.html" template is called, itincludes this "header.html" template (and the others) with a <spanclass="code"><xi:include /></span> tag, part of the Genshitemplating system. The "header.html" template is not a completelystatic HTML -- it also dynamically displays the current page name witha Genshi template method called "replace" with the code: <spanclass="code"><span py:replace="page"/></span>. It means replacethis <span class="code"><span /></span> region with the contentsfound in the variable 'page' that has been sent in the dictionary tothis "about.html" template, and is available through that namespace foruse by this "header.html" template. That's how it changes in the headerdepending on what page you are visiting.</p><p><strong><span class="code">sidebars.html</span></strong> - Thesidebars (navigation areas on the right side of the page) are generatedas two separate <span class="code">py:def</span> blocks in the"sidebars.html" template. The <span class="code">py:def</span>construct is best thought of as a "macro" code... a simple way toseparate and reuse common code snippets. All it takes to include theseon the "about.html" page template is to write <span class="code"><br /><br />$${sidebar_top()} <br />$${sidebar_bottom()} <br /><br /></span> in the page where they are wanted. CSS styling (in"/public/css/style.css") floats them off to the right side. You canremove a sidebar or add more of them, and the CSS will place them oneatop the other.</p><p>This is, of course, also exactly how the header and footertemplates are also displayed in their proper places, but we'll coverthat in the "master.html" template below.</p><p>Oh, and in sidebar_top we've added a dynamic menu that shows thelink to this page at the top when you're at the "index" page, and showsa link to the Home (index) page when you're here. Study the"sidebars.html" template to see how we used <span class="code">py:choose</span>for that.</p><p><strong><span class="code">footer.html</span></strong> - The"footer.html" block is simple, but also utilizes a special "replace"method to set the current YEAR in the footer copyright message. Thecode is: <span class="code"><spanpy:replace="now.strftime('%Y')"> </span> and it uses the variable "now"that was passed in with the dictionary of variables. But because "now"is a datetime object, we can use the Python <span class="code">"strftime()"</span>method with the "replace" call to say "Just Display The Year Here".Simple, elegant; we format the date display in the template (the Viewin the Model/View/Controller architecture) rather than formatting it inthe Controller method and sending it to the template as a stringvariable.</p><p><strong><span class="code">master.html</span></strong> - The"master.html" template is called last, by design. The "master.html"template controls the overall design of the page we're looking at,calling first the "header" py:def macro, then the putting everythingfrom this "about.html" template into the "content" div, and thencalling the "footer" macro at the end. Thus the "master.html" templateprovides the overall architecture for each page in this site.</p><p>But why then shouldn't we call it first? Isn't it the mostimportant? Perhaps, but that's precisely why we call it LAST. The"master.html" template needs to know where to find everything else,everything that it will use in py:def macros to build the page. So thatmeans we call the other templates first, and then call "master.html".</p><p>There's more to the "master.html" template... study it to seehow the <title> tags and static JS and CSS files are brought intothe page. Templating with Genshi is a powerful tool and we've onlyscratched the surface. There are also a few little CSS tricks hidden inthese pages, like the use of a "clearingdiv" to make sure that yourfooter stays below the sidebars and always looks right. That's not TG2at work, just CSS. You'll need all your skills to build a fine web app,but TG2 will make the hard parts easier so that you can concentratemore on good design and content rather than struggling with mechanics.</p></li></ol><p>Good luck with TurboGears 2!</p></div></body></html>