{{{ #!trachtml

Cook even better eggs


[http://trac-hacks.org/wiki/EggCookingTutorial/AdvancedEggCooking 原文] [[VisitCounter(TracDoc/AdvancedEggCookingja)]]

[wiki:TracDoc/BasicEggCookingJa TracDoc/BasicEggCookingJa] を読んで、ファーストeggをつくったのなら、
次はちょっと難しいことにチャレンジしましょう。

まず、他のTracの画面のように、表示に!ClearSilver のテンプレートをつかうよう改良しましょう。

1. テンプレートを追加するには

テンプレートを使うには、テンプレート用のディレクトリが必要です。
同じく単純に"Hello world!"という文字を表示するけど、今度はTracのレイアウトに載せるように改良します。

まず、以下のディレクトリを追加で作成してください:

./helloworld-plugin/helloworld/templates/

このディレクトリには、新しいファイルhelloworld.csを作成します:

#!text/html
<?cs include "header.cs" ?>
<?cs include "macros.cs" ?>

<div id="content" class="helloworld">
<h1>Hello world!</h1>
</div>

<?cs include "footer.cs" ?>

これで、プラグインのためのテンプレートができました。

2. Tracにテンプレートの場所を通知する。

このままでは、Tracはテンプレートがどこあるか気づきません。教えてあげましょう。これをやるには、helloworld.pyにITemplageProviderインタフェースを実装します。

4行目を以下のように変更します。

#!python
from trac.web.chrome import INavigationContributor


#!python
from trac.web.chrome import INavigationContributor, ITemplateProvider

続けて、9行目を以下のように変更します。

#!python
implements(INavigationContributor, IRequestHandler)


#!python
implements(INavigationContributor, IRequestHandler, ITemplateProvider)

23行目以降のprocess_requestメソッドは、以下の2行に入れ替えます。

#!python
def process_request(self, req):
return 'helloworld.cs', None

ファイルの最後で、テンプレートの場所を指示します。

#!python
# ITemplateProvider methods
def get_templates_dirs(self):
"""
Return the absolute path of the directory containing the provided
ClearSilver templates.
"""
from pkg_resources import resource_filename
return [resource_filename(__name__, 'templates')]

以下はheloworld.pyの完成版です:[[FootNote(訳注:このサンプルではget_htdocs_dirsを実装しませんが、この場合他のプラグインでget_htdocs_dirsを使う場合に異常動作するようです。[72]を参考にして空を返す実装をして下さい。)]]

#!python
# Helloworld plugin

from trac.core import *
from trac.web.chrome import INavigationContributor, ITemplateProvider
from trac.web.main import IRequestHandler
from trac.util import escape, Markup

class UserbaseModule(Component):
implements(INavigationContributor, IRequestHandler, ITemplateProvider)

# INavigationContributor methods
def get_active_navigation_item(self, req):
return 'helloworld'

def get_navigation_items(self, req):
yield 'mainnav', 'helloworld', Markup('<a xhref="%s">Hello</a>', self.env.href.helloworld())

# IRequestHandler methods
def match_request(self, req):
return req.path_info == '/helloworld'

def process_request(self, req):
return 'helloworld.cs', None

# ITemplateProvider methods
def get_templates_dirs(self):
"""
Return the absolute path of the directory containing the provided
ClearSilver templates.
"""
from pkg_resources import resource_filename
return [resource_filename(__name__, 'templates')]

3. eggにテンプレートをコピーする

最後に、作成したテンプレートのディレクトリをeggに含める手続きを行います。

setup.pyを以下のように変更してください:
(訳注:これ0.9.2のころと比べて変更されてますね。)

#!python
from setuptools import setup

PACKAGE = 'TracHelloworld'
VERSION = '0.1'

setup(name=PACKAGE,
version=VERSION,
packages=['helloworld'],
package_data={'helloworld' : ['templates/*.cs']},
entry_points={'trac.plugins': '%s = helloworld' % PACKAGE},
)

4. ビルドと配布

ビルド等は [wiki:TracDoc/BasicEggCookingJa TracDoc/BasicEggCookingJa] と完全に同じです。

今回は、メインナビゲーションバーのボタンを押すと、Tracのレイアウトに従った大きな"Hello world!"が表示されることでしょう。

5. 次は

基本的なテンプレートの追加を行ったので、サイトにひねりを加えましょう。スタイルシートやイメージのような静的コンテンツを追加します。
続きは、[wiki:TracDoc/AdvancedEggCooking2Ja TracDoc/AdvancedEggCooking2Ja] を読んでください。

 

[[FootNote]]

}}}