Changes between Version 5 and Version 6 of EggCookingTutorial/AdvancedEggCooking2


Ignore:
Timestamp:
Sep 5, 2005, 2:37:44 AM (19 years ago)
Author:
Shun-ichi Goto
Comment:

updated to adapt to new ITemplateProvider and added the whole of final code.

Legend:

Unmodified
Added
Removed
Modified
  • EggCookingTutorial/AdvancedEggCooking2

    v5 v6  
    5757Trac doesn't know where our fine stylesheet and image is located. So you have to tell it to Trac.
    5858
    59 Add static data path to end of our plugin in file ''helloworld.py'':
     59Add the following code at the tail in file ''helloworld.py'' which
     60implements {{{get_htdocs_dir()}}} to tell the static data path information for this plugin
     61with identical prefix 'helloworld'.
    6062{{{
    6163#!python
    62     def get_htdocs_dir(self):
     64    def get_htdocs_dirs(self):
    6365        """
    64         Return the absolute path of a directory containing additional
    65         static resources (such as images, style sheets, etc).
     66        Return a list of directories with static resources (such as style
     67        sheets, images, etc.)
     68
     69        Each item in the list must be a `(prefix, abspath)` tuple. The
     70        `prefix` part defines the path in the URL that requests to these
     71        resources are prefixed with.
     72       
     73        The `abspath` is the absolute path to the directory containing the
     74        resources on the local file system.
    6675        """
    6776        from pkg_resources import resource_filename
    68         return resource_filename(__name__, 'htdocs')   
     77        return [('helloworld', resource_filename(__name__, 'htdocs'))]
    6978}}}
    7079
     
    7584#!python
    7685    def process_request(self, req):
     86        add_stylesheet(req, 'helloworld/css/helloworld.css')
     87        return 'helloworld.cs', None
     88}}}
     89Note that prefix path 'helloworld/' specified by {{{get_htdocs_dirs()}}} should be used.
     90
     91And also import {{{add_stylesheet()}}} at the beginning of ''helloworld.py''.
     92{{{
     93#!python
     94from trac.web.chrome import INavigationContributor, ITemplateProvider, add_stylesheet
     95}}}
     96
     97== Complete version of code ==
     98The whole of final code is here:
     99{{{
     100#!python
     101# Helloworld plugin
     102
     103from trac.core import *
     104from trac.web.chrome import INavigationContributor, ITemplateProvider, add_stylesheet
     105from trac.web.main import IRequestHandler
     106from trac.util import escape
     107
     108class UserbaseModule(Component):
     109    implements(INavigationContributor, IRequestHandler, ITemplateProvider)
     110       
     111    # INavigationContributor methods
     112    def get_active_navigation_item(self, req):
     113        return 'helloworld'
     114               
     115    def get_navigation_items(self, req):
     116        yield 'mainnav', 'helloworld', '<a href="%s">Hello</a>' \
     117                                       % escape(self.env.href.helloworld())
     118       
     119    # IRequestHandler methods
     120    def match_request(self, req):
     121        return req.path_info == '/helloworld'
     122                                       
     123    def process_request(self, req):
    77124        add_stylesheet(req, 'css/helloworld.css')
    78125        return 'helloworld.cs', None
     126               
     127    # ITemplateProvider methods
     128    def get_templates_dirs(self):
     129        """
     130        Return the absolute path of the directory containing the provided
     131        ClearSilver templates.
     132        """
     133        from pkg_resources import resource_filename
     134        return [resource_filename(__name__, 'templates')]
     135   
     136    def get_htdocs_dirs(self):
     137        """
     138        Return a list of directories with static resources (such as style
     139        sheets, images, etc.)
     140
     141        Each item in the list must be a `(prefix, abspath)` tuple. The
     142        `prefix` part defines the path in the URL that requests to these
     143        resources are prefixed with.
     144       
     145        The `abspath` is the absolute path to the directory containing the
     146        resources on the local file system.
     147        """
     148        from pkg_resources import resource_filename
     149        return [('helloworld', resource_filename(__name__, 'htdocs'))]
    79150}}}
    80151