cheetah
近年のWebアプリ開発においてテンプレートエンジンは欠かすことが出来ません。 Pythonでは多くのテンプレートエンジンが存在しますが、本項ではcheetahを解説します。
実績のあるテンプレートエンジンと言われており、cheetahの名に恥じない速度が売りのようです。 下記リンクよりダウンロードし、インストールを行いましょう。
本サイトでは「utf-8」を使用しますのでとある準備を行います。 下記コードを「sitecustomize.py」というファイル名で保存し「"Pythonをインストールしたディレクトリ"/Lib/site-packages」に配置してください。 これによりデフォルトのエンコーディングが「utf-8」となり、エンコード時にエラーが発生しなくなります。
import sys
sys.setdefaultencoding('utf-8')
使用頻度の高いfor文から見てみましょう。 まずはテンプレートファイルの呼び出し元のコードを記述します。
# -*- coding: utf-8 -*-
from Cheetah.Template import Template
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
tpl = Template(file='c:/python/cheetah_for.tpl')
tpl.data = []
tpl.data.append(('1', 'cheetah', 'template'))
tpl.data.append(('2', 'python-izm', 'com'))
tpl.data.append(('3', 'cheetah', 'sample'))
return tpl.respond().encode('utf-8')
9行目でテンプレートのインスタンスを生成します。 引数にはテンプレートファイルのファイルパスを指定しましょう。 11行目から14行目でテンプレートファイル内で参照する値を追加しています。 最後に「utf-8」でエンコードして戻り値を返しましょう。
次はテンプレートファイルの記述です。
-- ファイル名:cheetah_for.tpl --
<html>
<head>
<title>
Cheetahテスト
</title>
</head>
<body>
<table border='1'>
#for $tr in $data
<tr>
#for $td in $tr
<td width='100' align='center'>
$td
</td>
#end for
</tr>
#end for
</table>
</body>
</html>
Pythonのコードと違い閉じタグが必要となるので気を付けましょう。
-- 実行結果 --
| 1 | cheetah | template |
| 2 | python-izm | com |
| 3 | cheetah | sample |
次はif文です。先程のコードのテンプレートファイルの設定を変更します。
# -*- coding: utf-8 -*-
from Cheetah.Template import Template
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
tpl = Template(file='c:/python/cheetah_if.tpl')
tpl.data = []
tpl.data.append(('1', 'cheetah', 'template'))
tpl.data.append(('2', 'python-izm', 'com'))
tpl.data.append(('3', 'cheetah', 'sample'))
return tpl.respond().encode('utf-8')
テンプレートファイルは下記の通りです。
-- ファイル名:cheetah_if.tpl --
<html>
<head>
<title>
Cheetahテスト
</title>
</head>
<body>
<table border='1'>
#for $tr in $data
#if $tr[1] == 'cheetah'
<tr>
#for $td in $tr
<td width='100' align='center'>
$td
</td>
#end for
</tr>
#end if
#end for
</table>
</body>
</html>
2列目が「cheetah」のもののみ出力する形です。for文と同じように閉じタグをきちんとつけましょう。
-- 実行結果 --
| 1 | cheetah | template |
| 3 | cheetah | sample |
変数の設定も可能なので必要に応じて利用しましょう。 先程と同様にテンプレートファイルの設定を変更します。
# -*- coding: utf-8 -*-
from Cheetah.Template import Template
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
tpl = Template(file='c:/python/cheetah_val.tpl')
tpl.data = []
tpl.data.append(('1', 'cheetah', 'template'))
tpl.data.append(('2', 'python-izm', 'com'))
tpl.data.append(('3', 'cheetah', 'sample'))
return tpl.respond().encode('utf-8')
テンプレートファイルの記述を行いましょう。
-- ファイル名:cheetah_val.tpl --
<html>
<head>
<title>
Cheetahテスト
</title>
</head>
<body>
<table border="1">
#for $tr in $data
#set $value = $tr[1]
#if $value == "python-izm"
<tr>
#for $td in $tr
<td width="100" align="center">
$td
</td>
#end for
</tr>
#end if
#end for
</table>
</body>
</html>
if文のテンプレートファイルとほぼ同じ記述ですが、一度変数へセットしてからif文でチェックしています。 処理は同様に2列目が「cheetah」のもののみ出力する形です。
-- 実行結果 --
| 1 | cheetah | template |
| 3 | cheetah | sample |
まだまだテンプレートエンジンはあります。次はgenshi!
