Changes between Version 19 and Version 20 of EggCookingTutorial/AdvancedEggCooking2
- Timestamp:
- Jan 25, 2016, 8:26:16 PM (7 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
EggCookingTutorial/AdvancedEggCooking2
v19 v20 1 1 [[TOC(heading=Egg Cooking Tutorial, EggCookingTutorial/BasicEggCooking, EggCookingTutorial/AdvancedEggCooking, EggCookingTutorial/AdvancedEggCooking2, EggCookingTutorial/publish)]] 2 2 3 = Cooking high-end eggs =3 = Cooking high-end eggs 4 4 5 Now you have pretty neat plugin already but let's add final twist and serve some static content like stylesheet andimage.5 Now that you have a working version of a plugin already, let's add a final twist and serve some static content like a stylesheet and an image. 6 6 7 == Important thing to check ==7 == Important thing to check 8 8 9 First step is to ensure that your ''trac.ini'' doesn't have ''htdocs_location'' set otherwise Trac can't serve static data.9 First step is to ensure that your ''trac.ini'' doesn't have ''htdocs_location'' set, otherwise Trac can't serve static data. 10 10 11 == More directories ==11 == More directories 12 12 13 Since we don't have enough directories in our simple plugin let's make few more:14 {{{ 13 We will need a few more directories for the files that we will add: 14 {{{#!sh 15 15 ./helloworld-plugin/helloworld/htdocs/ 16 16 ./helloworld-plugin/helloworld/htdocs/css/ … … 18 18 }}} 19 19 20 == Style is everything ==20 == Style is everything 21 21 22 We want to use our own stylesheet to give some color to our fine content. For that we need cascaded stylesheet.22 We want to use our own stylesheet to give some color to our content. For that we need [wikipedia:Cascading_Style_Sheets cascading stylesheets]. 23 23 24 24 Create ''helloworld.css'' in ''./helloworld-plugin/helloworld/htdocs/css/'': 25 {{{ 26 #!text/css 25 {{{#!text/css 27 26 div.helloworld h1 { 28 27 color: red; … … 30 29 }}} 31 30 32 == Image tells more than thousand words ==31 == Image tells more than thousand words 33 32 34 Images are always nice.33 Images enhance the look and feel of your site. Put small image named ''helloworld.jpg'' in ''./helloworld-plugin/helloworld/htdocs/images/''. 35 34 36 Put small image named ''helloworld.jpg'' in ''./helloworld-plugin/helloworld/htdocs/images/''. 37 38 Note: Since it's not practical to show jpg contents here you should find one image by yourself somewhere. 39 40 == Egg grows == 35 == Egg grows 41 36 42 37 Even natural eggs doesn't grow Python eggs does. 43 38 44 39 Modify ''setup.py'' to include our static data: 45 {{{ 46 #!python 40 {{{#!python 47 41 from setuptools import setup 48 42 … … 59 53 }}} 60 54 61 == Tell it to Trac too ==55 == Tell it to Trac 62 56 63 Trac doesn't know where our fine stylesheet and image is located. So you have to tell it to Trac.57 Trac doesn't know where our stylesheet and image is located, so you have to tell it to Trac. 64 58 65 Add the following code at the tail in file ''helloworld.py'' which 66 implements {{{get_htdocs_dir()}}} to tell the static data path information for this plugin 67 with identical prefix 'hw'. 68 {{{ 69 #!python 59 Add the following code at the tail in file ''helloworld.py'', which implements {{{get_htdocs_dir()}}} to tell the static data path information for this plugin with identical prefix 'hw': 60 {{{#!python 70 61 def get_htdocs_dirs(self): 71 62 """Return a list of directories with static resources (such as style … … 83 74 }}} 84 75 85 == Remember to load stylesheet ==76 == Remember to load stylesheet 86 77 87 78 To make Trac to load our stylesheet you need to modify ''process_request'' method starting from line 23 to following: 88 {{{ 89 #!python 79 {{{#!python 90 80 def process_request(self, req): 91 81 add_stylesheet(req, 'hw/css/helloworld.css') 92 82 return 'helloworld.cs', None 93 83 }}} 84 94 85 Note that prefix path 'hw/' specified by {{{get_htdocs_dirs()}}} should be used. 95 86 96 And also import {{{add_stylesheet()}}} at the beginning of ''helloworld.py''. 97 {{{ 98 #!python 87 And also import {{{add_stylesheet()}}} at the beginning of ''helloworld.py'': 88 {{{#!python 99 89 from trac.web.chrome import INavigationContributor, ITemplateProvider, \ 100 90 add_stylesheet 101 91 }}} 102 92 103 == Complete version of code ==104 The whole of final code is here: 105 {{{ 106 #!python93 == Complete version of code 94 95 The whole of the Python code is here: 96 {{{#!python 107 97 # Helloworld plugin 108 98 … … 155 145 }}} 156 146 157 == Back to images ==147 == Back to images 158 148 159 We need to add our image to t emplate to see it.149 We need to add our image to the template to see it. 160 150 161 151 Our new ''helloworld.cs'': 162 {{{ 163 #!text/html 152 {{{#!text/html 164 153 <?cs include "header.cs" ?> 165 154 <?cs include "macros.cs" ?> … … 173 162 }}} 174 163 175 == Compilation and deployment ==164 == Compilation and deployment 176 165 177 166 Now you should be familiar with both, so make an egg and deploy it. 178 167 179 Click and see ... Hello world! with your pretty own image.168 Click and see "Hello world!" with your chosen image. 180 169 181 == Aftermath ==170 == Aftermath 182 171 183 Now you have successfully completed nicesimple plugin that uses its own template and serves some static data.172 Now you have successfully completed a simple plugin that uses its own template and serves some static data.