Edgewall Software

Changes between Initial Version and Version 1 of 0.10/TracInterfaceCustomization


Ignore:
Timestamp:
Dec 6, 2007, 3:38:37 PM (16 years ago)
Author:
Christian Boos
Comment:

Copied from TracInterfaceCustomization@46

Legend:

Unmodified
Added
Removed
Modified
  • 0.10/TracInterfaceCustomization

    v1 v1  
     1= Customizing the Trac Interface =
     2[[TracGuideToc]]
     3
     4== Introduction ==
     5This page is meant to give users suggestions on how they can customize the look of Trac.  Topics on this page cover editing the HTML templates and CSS files, but not the program code itself.  The topics are intended to show users how they can modify the look of Trac to meet their specific needs.  Suggestions for changes to Trac's interface applicable to all users should be filed as tickets, not listed on this page.
     6
     7== Project Logo and Icon ==
     8The easiest parts of the Trac interface to customize are the logo and the site icon.  Both of these can be configured with settings in [wiki:TracIni trac.ini].
     9
     10The logo or icon image should be put in a folder named "htdocs" in your project's environment folder.  (''Note: in projects created with a Trac version prior to 0.9 you will need to create this folder'')
     11
     12 ''Note: you can actually put the logo and icon anywhere on your server (as long as it's accessible through the web server), and use their absolute or server-relative URLs in the configuration.''
     13
     14Now configure the appropriate section of your [wiki:TracIni trac.ini]:
     15
     16=== Logo ===
     17Change the `src` setting to `site/` followed by the name of your image file.  The `width` and `height` settings should be modified to match your image's dimensions (the Trac chrome handler uses "`site/`" for files within the project directory `htdocs` and "`common/`" for the common ones).
     18
     19{{{
     20[header_logo]
     21src = site/my_logo.gif
     22alt = My Project
     23width = 300
     24height = 100
     25}}}
     26
     27=== Icon ===
     28Icons should be a 16x16 image in `.gif` or `.ico` format.  Change the `icon` setting to `site/` followed by the name of your icon file.  Icons will typically be displayed by your web browser next to the site's URL and in the `Bookmarks` menu.
     29
     30{{{
     31[project]
     32icon = site/my_icon.ico
     33}}}
     34
     35Note though that this icon is ignored by Internet Explorer, which only accepts a file named ``favicon.ico`` at the root of the host. To make the project icon work in both IE and other browsers, you can store the icon in the document root of the host, and reference it from ``trac.ini`` as follows:
     36
     37{{{
     38[project]
     39icon = /favicon.ico
     40}}}
     41
     42== Site Appearance ==
     43
     44=== Trac 0.10 and below ===
     45
     46==== Site Header & Footer ====
     47In the environment folder for each Trac project there should be a directory called {{{templates}}}.  This folder contains files {{{site_header.cs}}} and {{{site_footer.cs}}}.  Users can customize their Trac site by adding the required HTML markup to these files.  The content of these two files will be placed immediately following the opening {{{<body>}}} tag and immediately preceding the closing {{{</body>}}} tag of each page in the site, respectively.
     48
     49These files may contain static HTML, though if users desire to have dynamically generated content they can make use of the [http://www.clearsilver.net/ ClearSilver] templating language from within the pages as well. When you need to see what variables are available to the template, append the query string `?hdfdump=1` to the URL of your Trac site. This will display a structured view of the template data.
     50
     51Static content, needs to be put outside the ClearSilver tag. For example:
     52
     53{{{
     54<?cs
     55#########################################################################
     56# Site footer - Contents are automatically inserted after main Trac HTML
     57?>
     58<h1>TEST FOOTER</h1>
     59}}}
     60
     61==== Site CSS ====
     62The primary means to adjust the layout of a Trac site is to add [http://www.w3.org/TR/REC-CSS2/ CSS] style rules that overlay the default rules. This is best done by editing the `site_css.cs` file in the enviroment's `templates` directory. The content of that template gets inserted into a `<style type="text/css"></style>` element on every HTML page generated by Trac.
     63
     64While you can add your custom style rules directly to the `site_css.cs` file, it is recommended that you simply reference an external style sheet, thereby enabling browsers to cache the CSS file instead of transmitting the rules with every response.
     65
     66The following example would import a style sheet located in the `style` root directory of your host:
     67{{{
     68@import url(/style/mytrac.css);
     69}}}
     70
     71You can use a !ClearSilver variable to reference a style sheet stored in the project environment's `htdocs` directory:
     72{{{
     73@import url(<?cs var:chrome.href ?>/site/style.css);
     74}}}
     75
     76Note that this references `htdocs/style.css`, not
     77`htdocs/site/style.css`.
     78
     79=== Trac 0.11 and above ===
     80
     81Since release [milestone:0.11], Trac is using [http://genshi.edgewall.org Genshi] as the templating engine.[[BR]]
     82Therefore, customization of the interface is achieved in a slightly different way than it used to be with the ClearSilver templating engine.
     83
     84Documentation is yet to be written, in the meantime the following tip should work.
     85
     86Say you want to add a link to a custom stylesheet, and then your own
     87header and footer.  Create a file {{{/path/to/env/templates/site.html}}} or {{{/path/to/inherit/option/templates_dir/site.html}}}, with contents like this:
     88
     89{{{
     90#!xml
     91<html xmlns="http://www.w3.org/1999/xhtml"
     92      xmlns:py="http://genshi.edgewall.org/"
     93      py:strip="">
     94
     95  <!--! Add site-specific style sheet -->
     96  <head py:match="head" py:attrs="select('@*')">
     97    ${select('*')}
     98    <link rel="stylesheet" type="text/css"
     99          href="${href.chrome('site/style.css')}" />
     100  </head>
     101
     102  <body py:match="body" py:attrs="select('@*')">
     103    <!--! Add site-specific header -->
     104    <div id="siteheader">
     105      <!--! Place your header content here... -->
     106    </div>
     107
     108    ${select('*|text()')}
     109
     110    <!--! Add site-specific footer -->
     111    <div id="sitefooter">
     112      <!--! Place your footer content here... -->
     113    </div>
     114  </body>
     115</html>
     116}}}
     117Note that this references your environment's `htdocs/style.css`.
     118
     119Example snippet of adding introduction text to the new ticket form (hide when preview):
     120
     121{{{
     122#!xml
     123<form py:match="div[@id='content' and @class='ticket']/form" py:attrs="select('@*')">
     124  <py:if test="req.environ['PATH_INFO'] == '/newticket' and (not 'preview' in req.args)">
     125    <p>Please make sure to search for existing tickets before reporting a new one!</p>
     126  </py:if>
     127  ${select('*')}
     128</form>
     129}}}
     130
     131If the environment is upgraded from 0.10 and a `site_newticket.cs` file already exists, it can actually be loaded by using a workaroud - providing it contains no ClearSilver processing. In addition, as only one element can be imported, the content needs some sort of wrapper such as a `<div>` block or other similar parent container. The XInclude namespace must be specified to allow includes, but that can be moved to document root along with the others:
     132{{{
     133#!xml
     134<form py:match="div[@id='content' and @class='ticket']/form" py:attrs="select('@*')"
     135        xmlns:xi="http://www.w3.org/2001/XInclude">
     136  <py:if test="req.environ['PATH_INFO'] == '/newticket' and (not 'preview' in req.args)">
     137    <xi:include href="site_newticket.cs"><xi:fallback /></xi:include>
     138  </py:if>
     139  ${select('*')}
     140</form>
     141}}}
     142
     143Also note that the `site.html` (despite its name) can be put in a common templates directory - see the `[inherit] templates_dir` option. This could provide easier maintainence (and a migration path from 0.10 for larger installations) as one new global `site.html` file can be made to include any existing header, footer and newticket snippets.
     144
     145== Project List ==
     146You can use a custom ClearSilver template to display the list of projects if you are using Trac with multiple projects. 
     147
     148The following is the basic template used by Trac to display a list of links to the projects.  For projects that could not be loaded it displays an error message. You can use this as a starting point for your own index template.
     149
     150{{{
     151#!text/html
     152<html>
     153<head><title>Available Projects</title></head>
     154<body>
     155 <h1>Available Projects</h1>
     156 <ul><?cs
     157 each:project = projects ?><li><?cs
     158  if:project.href ?>
     159   <a href="<?cs var:project.href ?>" title="<?cs var:project.description ?>">
     160    <?cs var:project.name ?></a><?cs
     161  else ?>
     162   <small><?cs var:project.name ?>: <em>Error</em> <br />
     163   (<?cs var:project.description ?>)</small><?cs
     164  /if ?>
     165  </li><?cs
     166 /each ?>
     167 </ul>
     168</body>
     169</html>
     170}}}
     171
     172Once you've created your custom template you will need to configure the webserver to tell Trac where the template is located:
     173
     174For [wiki:TracFastCgi FastCGI]:
     175{{{
     176FastCgiConfig -initial-env TRAC_ENV_PARENT_DIR=/parent/dir/of/projects \
     177              -initial-env TRAC_ENV_INDEX_TEMPLATE=/path/to/template
     178}}}
     179
     180For [wiki:TracModPython mod_python]:
     181{{{
     182PythonOption TracEnvIndexTemplate /path/to/template
     183}}}
     184
     185For [wiki:TracCgi CGI]:
     186{{{
     187SetEnv TRAC_ENV_INDEX_TEMPLATE /path/to/template
     188}}}
     189
     190For [wiki:TracStandalone], you'll need to set up the `TRAC_ENV_INDEX_TEMPLATE` environment variable in the shell used to launch tracd:
     191 - Unix
     192   {{{
     193#!sh
     194$ export TRAC_ENV_INDEX_TEMPLATE=/path/to/template
     195   }}}
     196 - Windows
     197   {{{
     198#!sh
     199$ set TRAC_ENV_INDEX_TEMPLATE=/path/to/template
     200   }}}
     201
     202== Main Templates ==
     203
     204It is also possible to use your own modified versions of the Trac [http://www.clearsilver.net/ ClearSilver] templates. Note though that this technique is not recommended because it makes upgrading Trac rather problematic: there are unfortunately several dependencies between the templates and the application code, such as the name of form fields and the structure of the template data, and these are likely to change between different versions of Trac.
     205
     206If you absolutely need to use modified templates, copy the template files from the default templates directory (usually in found in `$prefix/share/trac/templates`) into the `templates` directory of the project environment. Then modify those copies to get the desired results.
     207
     208
     209== Adding Google Adsense Services To Your Trac ==
     210=== Google Adsense For Content - Ads ===
     211It's actually pretty simple now that I know it, here it goes.
     212On your trac environment directory, edit '''`site_css.cs`''' and put this inside:
     213{{{
     214#!css
     215<?cs
     216##################################################################
     217# Site CSS - Place custom CSS, including overriding styles here.
     218?>
     219/* For Mozilla and Safari */
     220*>#content {
     221        padding: 0;
     222        margin: 15px 140px 0 15px;
     223        text-align: left;
     224        min-height: 770px;
     225}
     226
     227/* For IE */
     228* html #content {
     229        padding: 0;
     230        margin: 15px 140px 0 15px;
     231        text-align: left;
     232        height: 770px;
     233}
     234
     235#ads {
     236        position: absolute;
     237        top: 170px;
     238        right: 10px;
     239        width: 140px;
     240        z-index: 1;
     241        text-align: right;               
     242}
     243}}}
     244Now a bit of explanation. Settings are of course, of topic, if you have doubts, there's a CSS link above in this page, click it, even so I'll explain parts.
     245
     246First we override '''`#content`''', trac's main content div id so we can have space for our ads. The most important part is that '''`140px`''', our right margin. We also set a minimun height(`min-height`) for Mozilla and Safary browsers and `height` for IE browser or else the ads would be on top of the footer in case the wiki page is small enough, which explains why there are two '''`#content`'''. Because IE doesn't respect `min-height`, so it's just work arround that.
     247
     248The '''`width`''' in '''`#ads`''' should only be set if you intend to have multiple ad spaces, for example, a Skyscraper Google Ad, and also a 'Use Firefox...' that google also makes available. If it wasn't set, both ads would show up side by side, wich is not good '''`;]`'''. If you only intend on using a single Skyscrapper Google Ad, you should ommit '''`width`''', why, because if a user has the firefox Ad-Block plugin, and he blocks all ad content from google he'll see the wiki pages with the full width. Neat right, this way we make everyone happy.
     249
     250Now we'll need to add the Google Ads code somewhere right? Edit '''`site_footer.cs`''', and next is an example:
     251{{{
     252#!text/html
     253<?cs
     254#########################################################################
     255# Site footer - Contents are automatically inserted after main Trac HTML
     256?>
     257<div id="ads">
     258        <!--- Start of Google Ad Code -->
     259        YOU WOULD PASTE IT HERE
     260
     261
     262        <!--- End of Google Ad Code -->
     263</div>
     264}}}
     265
     266Another example is those ads moving up/down everytime you scroll:
     267{{{
     268#!text/html
     269<?cs
     270#########################################################################
     271# Site footer - Contents are automatically inserted after main Trac HTML
     272?>
     273<div id="ads">
     274        <!--- Start of Google Ad Code -->
     275        YOU WOULD PASTE IT HERE
     276        <!--- End of Google Ad Code -->
     277</div>
     278
     279<!-- *********************************************************************
     280     * You may use this code for free on any web page provided that
     281     * these comment lines and the following credit remain in the code.
     282     * From http://www.javascript-fx.com
     283     ****************************************************************  -->
     284<script type="text/javascript">
     285<!--
     286function JSFX_FloatTopRight()
     287{
     288        var startX = 10, startY = 160;
     289        var ns = (navigator.appName.indexOf("Netscape") != -1);
     290        var d = document;
     291        var px = document.layers ? "" : "px";
     292        function ml(id)
     293        {
     294                var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
     295                if(d.layers)el.style=el;
     296                el.sP=function(x,y){this.style.right=x+px;this.style.top=y+px;};
     297                el.x = startX; el.y = startY;
     298                return el;
     299        }
     300        window.stayTopRight=function()
     301        {
     302                var pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
     303                var dY = (pY > startY) ? pY : startY;
     304                ftlObj.y += (dY - ftlObj.y)/8;
     305                ftlObj.sP(ftlObj.x, ftlObj.y);
     306                setTimeout("stayTopRight()", 40);
     307        }
     308        ftlObj = ml("ads");
     309        stayTopRight();
     310}
     311JSFX_FloatTopRight();
     312//-->
     313</script>
     314}}}
     315
     316The most important line is this one:
     317{{{
     318ftlObj = ml("ads");
     319}}}
     320You must match your ads DIV name:
     321{{{
     322ftlObj = ml("<your_ads_div_name_here>");
     323}}}
     324
     325=== Google Adsense for Search - Search Boxes ===
     326As a follow up on the [http://projects.edgewall.com/trac/wiki/TracInterfaceCustomization#GoogleAdsenseForContentAds previous subject], I'll introduce one more way to add income to your Trac env, replacing trac's search box seen on the top right of trac's header for one that Google Adsense provides. First generate the code you need by logging into [http://google.com/adsense Google Adsense] and selecting "Google Adsense for Search", there you find enough info on how to generate it.
     327
     328Now we'll need to add an additional DIV to '''`site_css.cs`''', here it goes:
     329{{{
     330#!css
     331<?cs
     332##################################################################
     333# Site CSS - Place custom CSS, including overriding styles here.
     334?>
     335#GoogleSearch {
     336        position: absolute;
     337        top: 3px;
     338        right: 3px;
     339        z-index: 1;
     340}
     341}}}
     342
     343Now you'll need to add the previous generated search box HTML code somewhere. Edit '''`site_header.cs`''' to look something like:
     344{{{
     345#!text/html
     346<?cs
     347####################################################################
     348# Site header - Contents are automatically inserted above Trac HTML
     349?>
     350<div id="GoogleSearch">
     351        <!--- Start of Google Search Box Code -->
     352        YOU WOULD PASTE IT HERE
     353        <!--- End of Google Search Box Code -->
     354</div>
     355}}}
     356What happens is that the '''`#GoogleSearch`''' DIV is placed on top of Trac's search box, and by this, replacing it. Do NOTE that Trac's search will still be available, you'll just need to click the search button on the navbar.
     357
     358=== In Action ===
     359Want to see it in action, drop an eye on [http://dev.ufsoft.org/TracWikiTemplates Trac WikiTemplates Plugin] site.
     360
     361'''IMPORTANT: Do read Google's [https://www.google.com/adsense/localized-terms Terms & Conditions], your account will be frozen/deleted and you WON'T GET any amount you had previously earned if you don't FOLLOW THEM.'''
     362
     363----
     364See also TracGuide, TracIni