Genshi

Project Site: http://genshi.edgewall.org/

Project Lead: Christopher Lenz

Current Version: 0.3.0

Plugin Name: None (part of the genshi package)

Plugin Maintainer: -

Current Version: -

What Works: Master Template Replacement, Widget Usage, Normal Templating

What Doesn't: Widget Definition

Genshiはストリームベースのxmlテンプレーティングシステムで、Kidから強く影響を受けています。主に優れているのは、良いトレースバックと実行速度です。実装されている特徴にはいくつかの違いはありますが、 <?python ?> を含まないほとんどのプロジェクトは、文法がほとんど同じですからKidからGenshiに問題なく変換できるでしょう。アーキテクチャがKidに非常によく似ているので、Genshiはウィジェットも処理することができます。近い将来公式なTurboGearsのテンプレートライブラリとしてKidの代替品となる可能性があります。

プロジェクトのウェブサイトにはドキュメントがありますし、svnにはすべてGenshiを使用したTurboGearsアプリケーションのサンプルがあります。

インストール

easy_install Genshi する必要があるでしょう。あるいはeggを手動でダウンロードして easy_install -f ... Genshi してもOKです。これはテンプレートプラグインとして設定されるようにシステムに適切なプラグインフックを追加します。

このテンプレート例では studentspassing_score を扱います。それぞれの生徒は namescores リスト(すべての生徒で同じ大きさであるとします)、 average を持ちます。テンプレートはこれらの生徒をテーブルへレンダリングします。落第スコアにはCSSのクラスとして failing を付与し、生徒がいない場合には適切に処理します。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<!--! This would be saved as projectname/templates/students.html -->

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:xi="http://www.w3.org/2001/XInclude">

  <!--! Slightly different from Kid, all includes are done with XIncludes -->
  <xi:include href="master.html" />

  <head>
    <title>Template Example</title>
  </head>

  <body>
    <div py:choose="len(students)" py:strip="True">
      <table class="students" py:when="0">
        <tr>
          <th>No Students Found</th>
        </tr>
      </table>
      <table class="students" py:otherwise="">
        <tr>
          <th>Student Name</th>
          <th colspan="${len(students[0].scores)}">Scores</th>
          <th>Average</th>
        </tr>
        <tr py:for="student in students">
          <th py:content="student.name">Student Name</th>
          <span py:for="score in student.scores" py:choose="" py:strip="True">
            <td class="failing" py:when="score < passing_score">${score}</td>
            <td py:otherwise="">${score}</td>
          </span>

          <!--! alternative way of handling the test,
          yay for the and/or hack -->
          <td class="average ${student.average < passing_score and 'failing' or ''}">${student.average}</td>
        </tr>
      </table>
    </div>