Edgewall Software

This page documents the 0.10 release. Documentation for other releases can be found here.

Customizing the Trac Interface

Introduction

This 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.

Project Logo and Icon

The easiest parts of the Trac interface to customize are the logo and the site icon. Both of these can be configured with settings in trac.ini.

The 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)

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.

Now configure the appropriate section of your trac.ini:

Change 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).

[header_logo]
src = site/my_logo.gif
alt = My Project
width = 300
height = 100

Icon

Icons 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.

[project]
icon = site/my_icon.ico

Note 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:

[project]
icon = /favicon.ico

Site Appearance

Trac 0.10 and below

Site Header & Footer

In 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.

These files may contain static HTML, though if users desire to have dynamically generated content they can make use of the 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.

Static content, needs to be put outside the ClearSilver tag. For example:

<?cs
#########################################################################
# Site footer - Contents are automatically inserted after main Trac HTML
?>
<h1>TEST FOOTER</h1>

Site CSS

The primary means to adjust the layout of a Trac site is to add 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.

While 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.

The following example would import a style sheet located in the style root directory of your host:

@import url(/style/mytrac.css);

You can use a ClearSilver variable to reference a style sheet stored in the project environment's htdocs directory:

@import url(<?cs var:chrome.href ?>/site/style.css);

Note that this references htdocs/style.css, not htdocs/site/style.css.

Trac 0.11 and above

Since release 0.11, Trac is using Genshi as the templating engine.
Therefore, customization of the interface is achieved in a slightly different way than it used to be with the ClearSilver templating engine.

Documentation is yet to be written, in the meantime the following tip should work.

Say you want to add a link to a custom stylesheet, and then your own header and footer. Create a file /path/to/env/templates/site.html or /path/to/inherit/option/templates_dir/site.html, with contents like this:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/"
      py:strip="">

  <!--! Add site-specific style sheet -->
  <head py:match="head" py:attrs="select('@*')">
    ${select('*')}
    <link rel="stylesheet" type="text/css"
          href="${href.chrome('site/style.css')}" />
  </head>

  <body py:match="body" py:attrs="select('@*')">
    <!--! Add site-specific header -->
    <div id="siteheader">
      <!--! Place your header content here... -->
    </div>

    ${select('*|text()')}

    <!--! Add site-specific footer -->
    <div id="sitefooter">
      <!--! Place your footer content here... -->
    </div>
  </body>
</html>

Note that this references your environment's htdocs/style.css.

Example snippet of adding introduction text to the new ticket form (hide when preview):

<form py:match="div[@id='content' and @class='ticket']/form" py:attrs="select('@*')">
  <py:if test="req.environ['PATH_INFO'] == '/newticket' and (not 'preview' in req.args)">
    <p>Please make sure to search for existing tickets before reporting a new one!</p>
  </py:if>
  ${select('*')} 
</form>

If 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:

<form py:match="div[@id='content' and @class='ticket']/form" py:attrs="select('@*')"
        xmlns:xi="http://www.w3.org/2001/XInclude">
  <py:if test="req.environ['PATH_INFO'] == '/newticket' and (not 'preview' in req.args)"> 
    <xi:include href="site_newticket.cs"><xi:fallback /></xi:include>
  </py:if>
  ${select('*')} 
</form>

Also 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.

Project List

You can use a custom ClearSilver template to display the list of projects if you are using Trac with multiple projects.

The 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.

<html>
<head><title>Available Projects</title></head>
<body>
 <h1>Available Projects</h1>
 <ul><?cs
 each:project = projects ?><li><?cs
  if:project.href ?>
   <a href="<?cs var:project.href ?>" title="<?cs var:project.description ?>">
    <?cs var:project.name ?></a><?cs
  else ?>
   <small><?cs var:project.name ?>: <em>Error</em> <br />
   (<?cs var:project.description ?>)</small><?cs
  /if ?>
  </li><?cs
 /each ?>
 </ul>
</body>
</html>

Once you've created your custom template you will need to configure the webserver to tell Trac where the template is located:

For FastCGI:

FastCgiConfig -initial-env TRAC_ENV_PARENT_DIR=/parent/dir/of/projects \
              -initial-env TRAC_ENV_INDEX_TEMPLATE=/path/to/template

For mod_python:

PythonOption TracEnvIndexTemplate /path/to/template

For CGI:

SetEnv TRAC_ENV_INDEX_TEMPLATE /path/to/template

For TracStandalone, you'll need to set up the TRAC_ENV_INDEX_TEMPLATE environment variable in the shell used to launch tracd:

  • Unix
    $ export TRAC_ENV_INDEX_TEMPLATE=/path/to/template
    
  • Windows
    $ set TRAC_ENV_INDEX_TEMPLATE=/path/to/template
    

Main Templates

It is also possible to use your own modified versions of the Trac 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.

If 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.

Adding Google Adsense Services To Your Trac

Google Adsense For Content - Ads

It's actually pretty simple now that I know it, here it goes. On your trac environment directory, edit site_css.cs and put this inside:

<?cs
##################################################################
# Site CSS - Place custom CSS, including overriding styles here.
?>
/* For Mozilla and Safari */
*>#content {
        padding: 0;
        margin: 15px 140px 0 15px;
        text-align: left;
        min-height: 770px;
}

/* For IE */
* html #content {
        padding: 0;
        margin: 15px 140px 0 15px;
        text-align: left;
        height: 770px;
}

#ads {
        position: absolute;
        top: 170px;
        right: 10px;
        width: 140px;
        z-index: 1;
        text-align: right;               
}

Now 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.

First 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.

The 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.

Now we'll need to add the Google Ads code somewhere right? Edit site_footer.cs, and next is an example:

<?cs
#########################################################################
# Site footer - Contents are automatically inserted after main Trac HTML
?>
<div id="ads">
        <!--- Start of Google Ad Code -->
        YOU WOULD PASTE IT HERE


        <!--- End of Google Ad Code -->
</div>

Another example is those ads moving up/down everytime you scroll:

<?cs
#########################################################################
# Site footer - Contents are automatically inserted after main Trac HTML
?>
<div id="ads">
        <!--- Start of Google Ad Code -->
        YOU WOULD PASTE IT HERE
        <!--- End of Google Ad Code -->
</div>

<!-- *********************************************************************
     * You may use this code for free on any web page provided that 
     * these comment lines and the following credit remain in the code.
     * From http://www.javascript-fx.com
     ****************************************************************  -->
<script type="text/javascript">
<!--
function JSFX_FloatTopRight()
{
        var startX = 10, startY = 160;
        var ns = (navigator.appName.indexOf("Netscape") != -1);
        var d = document;
        var px = document.layers ? "" : "px";
        function ml(id)
        {
                var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
                if(d.layers)el.style=el;
                el.sP=function(x,y){this.style.right=x+px;this.style.top=y+px;};
                el.x = startX; el.y = startY;
                return el;
        }
        window.stayTopRight=function()
        {
                var pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
                var dY = (pY > startY) ? pY : startY;
                ftlObj.y += (dY - ftlObj.y)/8;
                ftlObj.sP(ftlObj.x, ftlObj.y);
                setTimeout("stayTopRight()", 40);
        }
        ftlObj = ml("ads");
        stayTopRight();
}
JSFX_FloatTopRight();
//-->
</script>

The most important line is this one:

ftlObj = ml("ads");

You must match your ads DIV name:

ftlObj = ml("<your_ads_div_name_here>");

Google Adsense for Search - Search Boxes

As a follow up on the 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 Google Adsense and selecting "Google Adsense for Search", there you find enough info on how to generate it.

Now we'll need to add an additional DIV to site_css.cs, here it goes:

<?cs
##################################################################
# Site CSS - Place custom CSS, including overriding styles here.
?>
#GoogleSearch {
        position: absolute;
        top: 3px;
        right: 3px;
        z-index: 1;
}

Now you'll need to add the previous generated search box HTML code somewhere. Edit site_header.cs to look something like:

<?cs
####################################################################
# Site header - Contents are automatically inserted above Trac HTML
?>
<div id="GoogleSearch">
        <!--- Start of Google Search Box Code -->
        YOU WOULD PASTE IT HERE
        <!--- End of Google Search Box Code -->
</div>

What 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.

In Action

Want to see it in action, drop an eye on Trac WikiTemplates Plugin site.

IMPORTANT: Do read Google's 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.


See also TracGuide, TracIni

Last modified 17 years ago Last modified on Dec 6, 2007, 3:38:37 PM
Note: See TracWiki for help on using the wiki.