Changes between Initial Version and Version 1 of EggCookingTutorial/AdvancedEggCooking


Ignore:
Timestamp:
Jul 28, 2005, 4:59:34 PM (19 years ago)
Author:
Jani Tiainen
Comment:

Initial article text

Legend:

Unmodified
Added
Removed
Modified
  • EggCookingTutorial/AdvancedEggCooking

    v1 v1  
     1= Cook even better eggs =
     2
     3After you read [wiki:EggCookingTutorial/BasicEggCooking EggCookingTutorial/BasicEggCooking] and created your first egg, it's time to make it a bit better.
     4
     5First we integrate our output to other Trac layout in form of !ClearSilver template.
     6
     7== Adding template ==
     8
     9To 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.
     10
     11For that we need to create one additional directory:
     12{{{
     13./helloworld-plugin/helloworld/templates/
     14}}}
     15
     16In that directory create new file ''helloworld.cs'':
     17{{{
     18<?cs include "header.cs" ?>
     19<?cs include "macros.cs" ?>
     20
     21<div id="content" class="helloworld">
     22 <h1>Hello world!</h1>
     23</div>
     24
     25<?cs include "footer.cs" ?>
     26}}}
     27
     28Now you have created template for plugin.
     29
     30== Tell Trac where template is ==
     31
     32Trac doesn't know where your template is so you have to tell it. This is done by adding ITemplateProvider method in ''helloworld.py''.
     33
     34So you change few lines as following:
     35
     36Line 4 is changed from
     37{{{
     38from trac.web.chrome import INavigationContributor
     39}}}
     40to
     41{{{
     42from trac.web.chrome import INavigationContributor, ITemplateProvider
     43}}}
     44
     45Line 9 is changed from
     46{{{
     47    implements(INavigationContributor, IRequestHandler)
     48}}}
     49{{{
     50    implements(INavigationContributor, IRequestHandler, ITemplateProvider)
     51}}}
     52
     53Starting from line 23 old ''process_request'' method is replaced by
     54{{{
     55    def process_request(self, req):
     56        return 'helloworld.cs', None
     57}}}
     58
     59And to end of file you need to tell where your template is located
     60{{{
     61    # ITemplateProvider methods
     62    def get_templates_dir(self):
     63        """
     64        Return the absolute path of the directory containing the provided
     65        ClearSilver templates.
     66        """
     67        from pkg_resources import resource_filename
     68        return resource_filename(__name__, 'templates')
     69}}}
     70
     71Complete version of ''helloworld.py'':
     72{{{
     73# Helloworld plugin
     74
     75from trac.core import *
     76from trac.web.chrome import INavigationContributor, ITemplateProvider
     77from trac.web.main import IRequestHandler
     78from trac.util import escape
     79
     80class UserbaseModule(Component):
     81    implements(INavigationContributor, IRequestHandler, ITemplateProvider)
     82       
     83    # INavigationContributor methods
     84    def get_active_navigation_item(self, req):
     85        return 'helloworld'
     86               
     87    def get_navigation_items(self, req):
     88        yield 'mainnav', 'helloworld', '<a href="%s">Hello</a>' \
     89                                       % escape(self.env.href.helloworld())
     90       
     91    # IRequestHandler methods
     92    def match_request(self, req):
     93        return req.path_info == '/helloworld'
     94                                       
     95    def process_request(self, req):
     96        return 'helloworld.cs', None
     97               
     98    # ITemplateProvider methods
     99    def get_templates_dir(self):
     100        """
     101        Return the absolute path of the directory containing the provided
     102        ClearSilver templates.
     103        """
     104        from pkg_resources import resource_filename
     105        return resource_filename(__name__, 'templates')
     106}}}
     107
     108== Copy template to egg ==
     109
     110Finally you have to include new template directory in egg.
     111
     112So change ''setup.py'' to be like:
     113{{{
     114from setuptools import setup
     115
     116PACKAGE = 'TracHelloworld'
     117VERSION = '0.1'
     118
     119setup(name=PACKAGE, version=VERSION, packages=['helloworld'],
     120        package_data={'helloworld' : ['templates/*.cs',]})
     121}}}
     122
     123== Building and deploying ==
     124
     125Building and deployment goes exactly same way as it was in previous tutorial [wiki:EggCookingTutorial/BasicEggCooking#Firstdeployment EggCookingTutorial/BasicEggCooking].
     126
     127Now you should see big "Hello world!" text integrated in Trac layout when you press that fancy button in main navigation bar.
     128
     129== Aftermath ==
     130
     131Now 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]