= Cook even better eggs =
After you read [wiki:EggCookingTutorial/BasicEggCooking EggCookingTutorial/BasicEggCooking] and created your first egg, it's time to make it a bit better.
First we integrate our output to other Trac layout in form of !ClearSilver template.
== Adding template ==
To have a template we need directory and of course template itself. We will keep same simple "Hello world!" text in visible, but this time we will integrate our fine words in Trac layout.
For that we need to create one additional directory:
{{{
./helloworld-plugin/helloworld/templates/
}}}
In that directory create new file ''helloworld.cs'':
{{{
#!text/html
Hello world!
}}}
Now you have created template for plugin.
== Tell Trac where template is ==
Trac doesn't know where your template is so you have to tell it. This is done by adding ITemplateProvider method in ''helloworld.py''.
So you change few lines as following:
Line 4 is changed from
{{{
#!python
from trac.web.chrome import INavigationContributor
}}}
to
{{{
#!python
from trac.web.chrome import INavigationContributor, ITemplateProvider
}}}
Line 9 is changed from
{{{
#!python
implements(INavigationContributor, IRequestHandler)
}}}
{{{
#!python
implements(INavigationContributor, IRequestHandler, ITemplateProvider)
}}}
Starting from line 23 old ''process_request'' method is replaced by
{{{
#!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_dir(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'':
{{{
#!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
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', 'Hello' \
% escape(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_dir(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 new template directory in egg.
So change ''setup.py'' to be like:
{{{
#!python
from setuptools import setup
PACKAGE = 'TracHelloworld'
VERSION = '0.1'
setup(name=PACKAGE, version=VERSION, packages=['helloworld'],
package_data={'helloworld' : ['templates/*.cs',]})
}}}
== Building and deploying ==
Building and deployment goes exactly same way as it was in previous tutorial [wiki:EggCookingTutorial/BasicEggCooking#Firstdeployment EggCookingTutorial/BasicEggCooking].
Now you should see big "Hello world!" text integrated in Trac layout when you press that fancy button in main navigation bar.
== Aftermath ==
Now you have added basic template for your plugin let's add final twist, put some static content like own stylesheet and one image. Continue to [wiki:EggCookingTutorial/AdvancedEggCooking2 EggCookingTutorial/AdvancedEggCooking2]