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. |
| 86 | add_stylesheet(req, 'helloworld/css/helloworld.css') |
| 87 | return 'helloworld.cs', None |
| 88 | }}} |
| 89 | Note that prefix path 'helloworld/' specified by {{{get_htdocs_dirs()}}} should be used. |
| 90 | |
| 91 | And also import {{{add_stylesheet()}}} at the beginning of ''helloworld.py''. |
| 92 | {{{ |
| 93 | #!python |
| 94 | from trac.web.chrome import INavigationContributor, ITemplateProvider, add_stylesheet |
| 95 | }}} |
| 96 | |
| 97 | == Complete version of code == |
| 98 | The whole of final code is here: |
| 99 | {{{ |
| 100 | #!python |
| 101 | # Helloworld plugin |
| 102 | |
| 103 | from trac.core import * |
| 104 | from trac.web.chrome import INavigationContributor, ITemplateProvider, add_stylesheet |
| 105 | from trac.web.main import IRequestHandler |
| 106 | from trac.util import escape |
| 107 | |
| 108 | class 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): |
| 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'))] |