Edgewall Software

Changes between Version 114 and Version 115 of GenshiTutorial


Ignore:
Timestamp:
Sep 9, 2008, 6:03:36 PM (16 years ago)
Author:
wjrogers@…
Comment:

Fixed broken syntax highlighting for listings under 'Adding a Layout Template' heading

Legend:

Unmodified
Added
Removed
Modified
  • GenshiTutorial

    v114 v115  
    718718 1. '''The Document Element'''
    719719
    720  {{{
    721  #!genshi
    722  <html xmlns="http://www.w3.org/1999/xhtml"
    723        xmlns:py="http://genshi.edgewall.org/" py:strip="">
    724  }}}
     720{{{
     721#!genshi
     722<html xmlns="http://www.w3.org/1999/xhtml"
     723    xmlns:py="http://genshi.edgewall.org/" py:strip="">
     724}}}
    725725
    726726 First, note that the root element of the template is an `<html>` tag. This is needed because markup templates are XML documents, and XML documents require a single root element (we also use it to attach our namespace declarations, but we could just as well do that on the nested `<py:match>` elements). However, because the page templates that include this file will also have `<html>` root elements, we add the `py:strip=""` directive so that this second `<html>` tag doesn't make it through into the output stream.
     
    728728 2. '''Match Template Definition'''
    729729
    730  {{{
    731  #!genshi
    732    <py:match path="head" once="true">
    733  }}}
     730{{{
     731#!genshi
     732<py:match path="head" once="true">
     733}}}
    734734
    735735 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.
     
    737737 3. '''Selecting Matched Content'''
    738738
    739  {{{
    740  #!genshi
    741     <head py:attrs="select('@*')">
     739{{{
     740#!genshi
     741<head py:attrs="select('@*')">
    742742}}}
    743743
    744744 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.
    745745
    746  {{{
    747  #!genshi
    748       <title py:with="title = list(select('title/text()'))">
    749         Geddit<py:if test="title">: ${title}</py:if>
    750       </title>
    751  }}}
     746{{{
     747#!genshi
     748<title py:with="title = list(select('title/text()'))">
     749    Geddit<py:if test="title">: ${title}</py:if>
     750</title>
     751}}}
    752752
    753753 This is a more complex example for selecting matched content: it fetches the text contained in the `<title>` element of the original `<head>` and prefixes it with the string “Geddit: ”. But as page templates may not even contain a `<title>` element, we first check whether it exists, and only add the colon if it does. Thus, if the page has no title of its own, the result will be “Geddit”.
    754754
    755  {{{
    756  #!genshi
    757       ${select('*[local-name()!="title"]')}
    758  }}}
     755{{{
     756#!genshi
     757${select('*[local-name()!="title"]')}
     758}}}
    759759
    760760 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.
     
    11141114</feed>
    11151115}}}
     1116
     1117
    11161118
    11171119Voila! We now provide Atom feeds for all our content.