Changes between Initial Version and Version 1 of EggCookingTutorial/AdvancedEggCooking2


Ignore:
Timestamp:
Jul 28, 2005, 5:03:12 PM (19 years ago)
Author:
Jani Tiainen
Comment:

Initial article text

Legend:

Unmodified
Added
Removed
Modified
  • EggCookingTutorial/AdvancedEggCooking2

    v1 v1  
     1= Advanced egg boiling part 2 =
     2
     3Now 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
     7First 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
     11Since 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
     20We want to use our own stylesheet to give some color to our fine content. For that we need cascaded stylesheet.
     21
     22Create ''helloworld.css'' in ''./helloworld-plugin/helloworld/htdocs/css/'':
     23{{{
     24div.helloworld h1 {
     25        color: red;
     26}
     27}}}
     28
     29== Image tells more than thousand words ==
     30
     31Images are always nice.
     32
     33Put small image named ''helloworld.jpg'' in ''./helloworld-plugin/helloworld/htdocs/images/''.
     34
     35Note: Since it's not practical to show jpg contents here you should find one image by yourself somewhere.
     36
     37== Egg grows ==
     38
     39Even natural eggs doesn't grow Python eggs does.
     40
     41Modify ''setup.py'' to include our static data:
     42{{{
     43from setuptools import setup
     44
     45PACKAGE = 'TracHelloworld'
     46VERSION = '0.1'
     47
     48setup(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
     55Trac doesn't know where our fine stylesheet and image is located. So you have to tell it to Trac.
     56
     57Add 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
     70To 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
     79We need to add our image to template to see it.
     80
     81Our 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
     96Now you should be familiar with both, so make an egg and deploy it.
     97
     98Click and see... Hello world! with your pretty own image.
     99
     100== Aftermath ==
     101
     102Now you have successfully completed nice simple plugin that uses it's own template and servers some static data.