| 3 | <h1 id="Cook even better eggs">Cook even better eggs</h1> <br /> [http://trac-hacks.org/wiki/EggCookingTutorial/AdvancedEggCooking 原文] <br /> <p><strike>After you read [wiki:EggCookingTutorial/BasicEggCooking EggCookingTutorial/BasicEggCooking] and created your first egg, it's time to make it a bit better.<br /> </strike>[wiki:TracDoc/BasicEggCookingJa TracDoc/BasicEggCookingJa] を読んで、ファーストeggをつくったのなら、<br /> 次はちょっと難しいことにチャレンジしましょう。<br /> </p> <p><strike>First we integrate our output to other Trac layout in form of !ClearSilver template.<br /> </strike>他のTracの画面のように、表示に!ClearSilver のテンプレートをつかうよう改良しましょう。<br /> </p> <h2 id="Adding template">Adding template</h2> <p><strike>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.</strike><br /> テンプレートを使うには、テンプレート用のディレクトリが必要です。<br /> 同じく単純に"Hello world!"という文字を表示するけど、今度はTracの例ストに載せるように改良します。<br /> </p> <p><strike>For that we need to create one additional directory:</strike><br /> ます、以下のディレクトリを追加で作成してください:<br /> </p> <pre class="tmtrac">./helloworld-plugin/helloworld/templates/<br /></pre> <p><strike>In that directory create a new file <em>helloworld.cs</em>:<br /> </strike>このディレクトリには、新しいファイルhelloworld.csを作成します:<br /> </p> <pre class="tmtrac">#!text/html<br /><?cs include "header.cs" ?><br /><?cs include "macros.cs" ?><br /><br /><div id="content" class="helloworld"><br /> <h1>Hello world!</h1><br /></div><br /><br /><?cs include "footer.cs" ?><br /></pre> <p><strike>Now you have created the template for the plugin.</strike><br /> これで、プラグインのためのテンプレートができました。<br /> </p> <h2 id="Tell Trac where template is">Tell Trac where template is</h2> <p><strike>Trac doesn't know where your template is so you have to tell it. This is done by implementing the ITemplateProvider interface in <em>helloworld.py</em>.</strike><br /> このままでは、Tracはテンプレートがどこあるか気づきません。教えてあげましょう。これをやるには、helloworld.pyにITemplageProviderインタフェースを実装します。<br /> </p> <p><strike>So you change few lines as following:</strike><br /> </p> <p><strike>Line 4 is changed from<br /> </strike>4行目を以下のように変更します。<br /> </p> <pre class="tmtrac">#!python<br />from trac.web.chrome import INavigationContributor<br /></pre> <p><strike>to</strike><br /> ↓<br /> </p> <pre class="tmtrac">#!python<br />from trac.web.chrome import INavigationContributor, ITemplateProvider<br /></pre> <p><strike>Line 9 is changed from</strike><br /> 9行目を以下のように変更します。<br /> </p> <pre class="tmtrac">#!python<br /> implements(INavigationContributor, IRequestHandler)<br /></pre> <p>↓<br /> </p> <pre class="tmtrac">#!python<br /> implements(INavigationContributor, IRequestHandler, ITemplateProvider)<br /></pre> <p><strike>Starting from line 23 old <em>process_request</em> method is replaced by</strike><br /> 23行目以降のprocess_requestメソッドは、以下の2行に入れ替えます。<br /> </p> <pre class="tmtrac">#!python<br /> def process_request(self, req):<br /> return 'helloworld.cs', None<br /></pre> <p><strike>And to end of file you need to tell where your template is located</strike><br /> ファイルの最後で、テンプレートの場所を指示します。<br /> </p> <pre class="tmtrac">#!python<br /> # ITemplateProvider methods<br /> def get_templates_dirs(self):<br /> """<br /> Return the absolute path of the directory containing the provided<br /> ClearSilver templates.<br /> """<br /> from pkg_resources import resource_filename<br /> return [resource_filename(__name__, 'templates')]<br /></pre> <p><strike>Complete version of <em>helloworld.py</em>:</strike><br /> 以下はheloworld.pyの完成版です:<br /> </p> <pre class="tmtrac">#!python<br /># Helloworld plugin<br /><br />from trac.core import *<br />from trac.web.chrome import INavigationContributor, ITemplateProvider<br />from trac.web.main import IRequestHandler<br />from trac.util import escape, Markup<br /><br />class UserbaseModule(Component):<br /> implements(INavigationContributor, IRequestHandler, ITemplateProvider)<br /> <br /> # INavigationContributor methods<br /> def get_active_navigation_item(self, req):<br /> return 'helloworld'<br /> <br /> def get_navigation_items(self, req):<br /> yield 'mainnav', 'helloworld', Markup('<a xhref="%s">Hello</a>', self.env.href.helloworld())<br /> <br /> # IRequestHandler methods<br /> def match_request(self, req):<br /> return req.path_info == '/helloworld'<br /> <br /> def process_request(self, req):<br /> return 'helloworld.cs', None<br /> <br /> # ITemplateProvider methods<br /> def get_templates_dirs(self):<br /> """<br /> Return the absolute path of the directory containing the provided<br /> ClearSilver templates.<br /> """<br /> from pkg_resources import resource_filename<br /> return [resource_filename(__name__, 'templates')]<br /></pre> <h2 id="Copy template to egg">Copy template to egg</h2> <p><strike>Finally you have to include the new template directory in an egg.</strike><br /> 最後に、作成したテンプレートのディレクトリをeggに含める手続きを行います。<br /> </p> <p><strike>So change <em>setup.py</em> to be like:</strike><br /> setup.pyを以下のように変更してください:<br /> (訳注:これ0.9.2のころと比べて変更されてますね。)<br /> </p> <pre class="tmtrac">#!python<br />from setuptools import setup<br /><br />PACKAGE = 'TracHelloworld'<br />VERSION = '0.1'<br /><br />setup(name=PACKAGE,<br /> version=VERSION,<br /> packages=['helloworld'],<br /> package_data={'helloworld' : ['templates/*.cs']},<br /> entry_points={'trac.plugins': '%s = helloworld' % PACKAGE},<br />)<br /></pre> <h2 id="Building and deploying">Building and deploying</h2> <p><strike>Building and deployment goes exactly the same as it did in the previous tutorial [wiki:EggCookingTutorial/BasicEggCooking#Firstdeployment EggCookingTutorial/BasicEggCooking] .</strike><br /> ビルド等は [wiki:TracDoc/BasicEggCookingJa TracDoc/BasicEggCookingJa] と完全に同じです。 </p> <p><strike>Now you should see a big "Hello world!" integrated into your Trac layout when you press that fancy button in the main navigation bar.</strike><br /> 今回は、メインナビゲーションバーのボタンを押すと、Tracのレイアウトに従った大きな"Hello world!"が表示されることでしょう。<br /> </p> <h2 id="Aftermath">Aftermath</h2> <p><strike>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] </strike><br /> 基本的なテンプレートの追加を行ったので、サイトにひねりを加えましょう。スタイルシートやイメージのような静的コンテンツを追加します。<br /> <span class="missing wiki">続きは、</span>[wiki:TracDoc/BasicEggCooking2Ja TracDoc/BasicEggCooking2Ja] を読んでください。 </p> <p> </p> <p> </p> |