{{{ #!trachtml

Cook even better eggs


[http://trac-hacks.org/wiki/EggCookingTutorial/AdvancedEggCooking 原文]

After you read [wiki:EggCookingTutorial/BasicEggCooking EggCookingTutorial/BasicEggCooking] and created your first egg, it's time to make it a bit better.
[wiki:TracDoc/BasicEggCookingJa TracDoc/BasicEggCookingJa] を読んで、ファーストeggをつくったのなら、
次はちょっと難しいことにチャレンジしましょう。

First we integrate our output to other Trac layout in form of !ClearSilver template.
他のTracの画面のように、表示に!ClearSilver のテンプレートをつかうよう改良しましょう。

Adding template

To have a template we need a directory and of course the template itself. We will keep the same simple "Hello world!" text, but this time we will integrate our fine words into a Trac layout.
テンプレートを使うには、テンプレート用のディレクトリが必要です。
同じく単純に"Hello world!"という文字を表示するけど、今度はTracの例ストに載せるように改良します。

For that we need to create one additional directory:
ます、以下のディレクトリを追加で作成してください:

./helloworld-plugin/helloworld/templates/

In that directory create a new file helloworld.cs:
このディレクトリには、新しいファイル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" ?>

Now you have created the template for the plugin.
これで、プラグインのためのテンプレートができました。

Tell Trac where template is

Trac doesn't know where your template is so you have to tell it. This is done by implementing the ITemplateProvider interface in helloworld.py.
このままでは、Tracはテンプレートがどこあるか気づきません。教えてあげましょう。これをやるには、helloworld.pyにITemplageProviderインタフェースを実装します。

So you change few lines as following:

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

#!python
from trac.web.chrome import INavigationContributor

to

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

Line 9 is changed from
9行目を以下のように変更します。

#!python
implements(INavigationContributor, IRequestHandler)


#!python
implements(INavigationContributor, IRequestHandler, ITemplateProvider)

Starting from line 23 old process_request method is replaced by
23行目以降のprocess_requestメソッドは、以下の2行に入れ替えます。

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

And to end of file you need to tell where your template is located
ファイルの最後で、テンプレートの場所を指示します。

#!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')]

Complete version of helloworld.py:
以下はheloworld.pyの完成版です:

#!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')]

Copy template to egg

Finally you have to include the new template directory in an egg.
最後に、作成したテンプレートのディレクトリをeggに含める手続きを行います。

So change setup.py to be like:
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},
)

Building and deploying

Building and deployment goes exactly the same as it did in the previous tutorial [wiki:EggCookingTutorial/BasicEggCooking#Firstdeployment EggCookingTutorial/BasicEggCooking] .
ビルド等は [wiki:TracDoc/BasicEggCookingJa TracDoc/BasicEggCookingJa] と完全に同じです。

Now you should see a big "Hello world!" integrated into your Trac layout when you press that fancy button in the main navigation bar.
今回は、メインナビゲーションバーのボタンを押すと、Tracのレイアウトに従った大きな"Hello world!"が表示されることでしょう。

Aftermath

Now that you have added a basic template for your plugin let's add the final twist, putting some static content like a stylesheet and an image. Continue to [wiki:EggCookingTutorial/AdvancedEggCooking2 EggCookingTutorial/AdvancedEggCooking2]
基本的なテンプレートの追加を行ったので、サイトにひねりを加えましょう。スタイルシートやイメージのような静的コンテンツを追加します。
続きは、[wiki:TracDoc/BasicEggCooking2Ja TracDoc/BasicEggCooking2Ja] を読んでください。

 

 

}}}