| 1 | = Advanced egg boiling part 2 = |
| 2 | |
| 3 | Now you have pretty neat plugin already but let's add final twist and serve some static content like stylesheet and image. |
| 4 | |
| 5 | == Important thing to check == |
| 6 | |
| 7 | First step is to ensure that your ''trac.ini'' doesn't have ''htdocs_location'' set otherwise Trac can't serve static data. |
| 8 | |
| 9 | == More directories == |
| 10 | |
| 11 | Since we don't have enough directories in our simple plugin let's make few more: |
| 12 | {{{ |
| 13 | ./helloworld-plugin/helloworld/htdocs/ |
| 14 | ./helloworld-plugin/helloworld/htdocs/css/ |
| 15 | ./helloworld-plugin/helloworld/htdocs/images/ |
| 16 | }}} |
| 17 | |
| 18 | == Style is everything == |
| 19 | |
| 20 | We want to use our own stylesheet to give some color to our fine content. For that we need cascaded stylesheet. |
| 21 | |
| 22 | Create ''helloworld.css'' in ''./helloworld-plugin/helloworld/htdocs/css/'': |
| 23 | {{{ |
| 24 | div.helloworld h1 { |
| 25 | color: red; |
| 26 | } |
| 27 | }}} |
| 28 | |
| 29 | == Image tells more than thousand words == |
| 30 | |
| 31 | Images are always nice. |
| 32 | |
| 33 | Put small image named ''helloworld.jpg'' in ''./helloworld-plugin/helloworld/htdocs/images/''. |
| 34 | |
| 35 | Note: Since it's not practical to show jpg contents here you should find one image by yourself somewhere. |
| 36 | |
| 37 | == Egg grows == |
| 38 | |
| 39 | Even natural eggs doesn't grow Python eggs does. |
| 40 | |
| 41 | Modify ''setup.py'' to include our static data: |
| 42 | {{{ |
| 43 | from setuptools import setup |
| 44 | |
| 45 | PACKAGE = 'TracHelloworld' |
| 46 | VERSION = '0.1' |
| 47 | |
| 48 | setup(name=PACKAGE, version=VERSION, packages=['helloworld'], |
| 49 | package_data={'helloworld' : ['htdocs/css/*.css', 'htdocs/images/*.jpg', |
| 50 | 'templates/*.cs' ]}) |
| 51 | }}} |
| 52 | |
| 53 | == Tell it to Trac too == |
| 54 | |
| 55 | Trac doesn't know where our fine stylesheet and image is located. So you have to tell it to Trac. |
| 56 | |
| 57 | Add static data path to end of our plugin in file ''helloworld.py'': |
| 58 | {{{ |
| 59 | def get_htdocs_dir(self): |
| 60 | """ |
| 61 | Return the absolute path of a directory containing additional |
| 62 | static resources (such as images, style sheets, etc). |
| 63 | """ |
| 64 | from pkg_resources import resource_filename |
| 65 | return resource_filename(__name__, 'htdocs') |
| 66 | }}} |
| 67 | |
| 68 | == Remember to load stylesheet == |
| 69 | |
| 70 | To make Trac to load our stylesheet you need to modify ''process_request'' method starting from line 23 to following: |
| 71 | {{{ |
| 72 | def process_request(self, req): |
| 73 | add_stylesheet(req, 'css/helloworld.css') |
| 74 | return 'helloworld.cs', None |
| 75 | }}} |
| 76 | |
| 77 | == Back to images == |
| 78 | |
| 79 | We need to add our image to template to see it. |
| 80 | |
| 81 | Our new ''helloworld.cs'': |
| 82 | {{{ |
| 83 | <?cs include "header.cs" ?> |
| 84 | <?cs include "macros.cs" ?> |
| 85 | |
| 86 | <div id="content" class="helloworld"> |
| 87 | <h1>Hello world!</h1> |
| 88 | <img src="<?cs var:htdocs_location ?>images/helloworld.jpg"> |
| 89 | </div> |
| 90 | |
| 91 | <?cs include "footer.cs" ?> |
| 92 | }}} |
| 93 | |
| 94 | == Compilation and deployment == |
| 95 | |
| 96 | Now you should be familiar with both, so make an egg and deploy it. |
| 97 | |
| 98 | Click and see... Hello world! with your pretty own image. |
| 99 | |
| 100 | == Aftermath == |
| 101 | |
| 102 | Now you have successfully completed nice simple plugin that uses it's own template and servers some static data. |