Changes between Version 114 and Version 115 of GenshiTutorial
- Timestamp:
- Sep 9, 2008, 6:03:36 PM (16 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
GenshiTutorial
v114 v115 718 718 1. '''The Document Element''' 719 719 720 721 722 723 724 720 {{{ 721 #!genshi 722 <html xmlns="http://www.w3.org/1999/xhtml" 723 xmlns:py="http://genshi.edgewall.org/" py:strip=""> 724 }}} 725 725 726 726 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. … … 728 728 2. '''Match Template Definition''' 729 729 730 731 732 733 730 {{{ 731 #!genshi 732 <py:match path="head" once="true"> 733 }}} 734 734 735 735 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. … … 737 737 3. '''Selecting Matched Content''' 738 738 739 740 741 739 {{{ 740 #!genshi 741 <head py:attrs="select('@*')"> 742 742 }}} 743 743 744 744 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. 745 745 746 747 748 749 750 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 }}} 752 752 753 753 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”. 754 754 755 756 757 758 755 {{{ 756 #!genshi 757 ${select('*[local-name()!="title"]')} 758 }}} 759 759 760 760 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. … … 1114 1114 </feed> 1115 1115 }}} 1116 1117 1116 1118 1117 1119 Voila! We now provide Atom feeds for all our content.