Edgewall Software

Changes between Version 24 and Version 25 of GenshiTutorial


Ignore:
Timestamp:
Aug 29, 2007, 11:24:49 PM (17 years ago)
Author:
cmlenz
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • GenshiTutorial

    v24 v25  
    664664 }}}
    665665
    666  Here we define the first match template. The `path` attribute contains the XPath pattern the specifies which elements this match template should be applied to. In this case, the XPath is very simple: it matches any element with the local name “head”, so it will be applied to the `<head>...</head>` element (whether it's in the XHTML namespace or not). We also add the `once="true"` attribute to tell Genshi that we only expect a single occurrence of the `<head>` element in the stream. Genshi can perform some optimizations based on this information.
     666 Here we define the first match template. The `path` attribute contains an XPath pattern specifying which elements this match template should be applied to. In this case, the XPath is very simple: it matches any element with the tag name “head”, so it will be applied to the `<head>...</head>` element. We also add the `once="true"` attribute to tell Genshi that we only expect a single occurrence of the `<head>` element in the stream. Genshi can perform some optimizations based on this information.
    667667
    668668 3. '''Selecting Matched Content'''
     
    673673}}}
    674674
    675  Inside match templates, you can use the special function `select(path)` to access the element that matched the pattern. Here we use that function in the `py:attrs` directive, which basically translates to “''get all attributes defined on the matched element, and add them to this element''”. So for example if your page template contained `<head id="foo">`, the element produced by this match template would also have the same `id="foo"` attribute.
     675 Inside match templates, you can use the special function `select(path)` to access the element that matched the pattern. Here we use that function in the `py:attrs` directive, which basically translates to “''get all attributes on the matched element, and add them to this element''”. So for example if your page template contained `<head id="foo">`, the element produced by this match template would also have the same `id="foo"` attribute.
    676676
    677677 {{{
     
    689689 }}}
    690690
    691  Finally, this is an example for using a more complex XPath pattern. This `select()` incantation here returns a stream that contains all child elements of the original `<head>`, except for those elements that have the local name “title”. If we didn't add that predicate, the output stream would contain two `<title>` tags, and we don't want that.
    692 
    693 If you've done a bit of XSLT, match templates should look familiar. Otherwise, you may want to familiarize yourself with the basics of [http://en.wikipedia.org/wiki/XPath XPath 1]—note though that Genshi only implements a subset of the full spec as explained in [wiki:Documentation/xpath.html Using XPath in Genshi]. And just play around with match templates; at the core, the concept is actually very simple and consistent.
    694 
    695 Now we need to update the page templates: they no longer need the header and footer, and we'll have to include the `layout.html` file. For the inclusion, we add the namespace prefix for XInclude, and an `xi:include` element.
     691 Finally, this is an example for using a more complex XPath pattern. This `select()` incantation here returns a stream that contains all child elements of the original `<head>`, except for those elements with the tag name “title”. If we didn't add that predicate, the output stream would contain two `<title>` tags.
     692
     693If you've done a bit of XSLT, match templates should look familiar. Otherwise, you may want to familiarize yourself with the basics of [http://en.wikipedia.org/wiki/XPath XPath 1]—but note that Genshi only implements a subset of the full spec as explained in [wiki:Documentation/xpath.html Using XPath in Genshi]. Just play around with match templates a bit; at the core, the concept is actually pretty simple and consistent.
     694
     695Now we need to update the page templates: they no longer need the header and footer, and we'll have to include the `layout.html` file so that the match templates are applied. For the inclusion, we add the namespace prefix for XInclude, and an `xi:include` element.
    696696
    697697Let's see how the template should look now for `index.html`:
     
    720720}}}
    721721
    722 You need to also change `submit.html` analogously.
    723 
    724 Talking of “layout”, you can see that we've added references to some static resources in the layout template: there's an embedded image as well as a linked stylesheet and javascript file. [http://svn.edgewall.org/repos/genshi/trunk/examples/tutorial/geddit/static Download] those files and put them in your `geddit/static/` directory.
     722Also change the `submit.html` template analogously, by adding the namespace prefix, the `<xi:include>` element, and by removing the header and footer `<div>`s.
     723
     724Speaking of “layout”, you can see that we've added references to some static resources in the layout template: there's an embedded image as well as a linked stylesheet and javascript file. [http://svn.edgewall.org/repos/genshi/trunk/examples/tutorial/geddit/static Download] those files and put them in your `geddit/static/` directory.
    725725
    726726When you reload the front page in your browser, you should now see something similar to the following: