Edgewall Software

Changes between Version 29 and Version 30 of GenshiTutorial


Ignore:
Timestamp:
Aug 30, 2007, 1:52:27 PM (17 years ago)
Author:
cmlenz
Comment:

Notes on basic template example

Legend:

Unmodified
Added
Removed
Modified
  • GenshiTutorial

    v29 v30  
    9898{{{
    9999#!genshi
    100 <html xmlns="http://www.w3.org/1999/xhtml"
    101       xmlns:py="http://genshi.edgewall.org/">
     100<html xmlns="http://www.w3.org/1999/xhtml">
    102101  <head>
    103102    <title>$title</title>
     
    118117}}}
    119118
    120 This is basically an almost static HTML file with some simple variable substitution.
     119This is basically an almost static XHTML file with some simple variable substitution: the string `$title` will be replaced by a variable of that name that we pass into the template from the controller.
     120
     121There are couple of important things to point out here:
     122 * Variables substituted into templates, such as `$title` in our example, can be of any Python data type. Genshi will convert the value to a string and insert the result into the generated output stream.
     123 * You generally do not need to worry about XML-escaping such variables. Genshi will automatically take care of that when the template is serialized. We'll look into the details of this process later.
     124 * The template will be parsed by Genshi using an XML parser, which means that '''it needs to be well-formed XML'''. If you know HTML but are unfamiliar with XML/XHTML, you will need to read up on the topic. Here are a couple of good references:
     125   * [http://www.w3schools.com/xhtml/xhtml_html.asp Differences Between XHTML And HTML] at W3Schools
     126   * [http://www.sitepoint.com/article/xhtml-introduction/2 XHTML - An Introduction] at !SitePoint
     127   * [http://www.webmonkey.com/00/50/index2a.html XHTML Overview] at Webmonkey
     128 * That the template uses XHTML does not mean that your web-application will generate XHTML! You can choose whether you'd rather just generate good old HTML 4.01, because despite all the hype, that's still the format that works best in most browsers (see [http://webkit.org/blog/?p=68 this blog post] over at Surfin' Safari for some background).
    121129
    122130We now need to change the controller code so that this template is used. First, add the Genshi `TemplateLoader` to the imports at the top of the `geddit/controller.py` file, and instantiate a loader for the `geddit/templates` directory: