jump to content

Satheesh Babu

If you use Zope for managing your site's content, chances are that you use DTML Templates heavily. This explains how you can make relative links to common elements used in headers and footers.

Using DTML templates

Typically, your DTML Document has the structure


The standard_html_header and standard_html_footer are DTML methods you define in Zope to get you a consistent look through-out the site.In your header, let us say you have a logo image, some navigation links etc. Let us focus on how can we link to your logo image, stored as ZopeRoot/images/mylogo.gif.

Absolute, fully qualified URLs

Usually what you can do is to make these references absolute with the fully qualified URLs. If you use different servers for development and production, fully qualified URLs are not ideal - well, absolute links are not ideal in any case.

If you have a development server like www.mydevelopment.com, when you take the content over to production site, you'll need to remake these links.
Possible Solutions
  1. Write Perl (yuck!) or Python script to convert your pages!
  2. Create a DTML Method at the top level. Let us call this siteroot. This has only one line called http://www.myproductionsite.com/ in the production server, and http://www.mydevelopment.com/ in the development server.
    And you write your link as <dtml-var siteroot>images/mylogo.gif instead of http://www.myproductionsite.com/images/mylogo.gif. Now, when you are in production server, Zope serves up the HTML with that server name and when you are in the development server, Zope serves the HTML with the development server name.
    Improvisation - Zope already has the server name pre-defined as BASE0. So, you can change your siteroot method to have <dtml-var BASE0>. That should work in any Zope server.

Absolute URLs from domain name root

The second best thing to relative URLs is where you don't specify the server name, but make all your links with respect to root of the domain (/). This will require reworking if your root URL canges.

What if some suite decides that your site has to be under some site like www.bigbadtakeover.com/myproductionsite/? You will have to and change the reference like /myproductionsite/images/mylogo.gif
Possible Solution
Improvising our previous siteroot DTML method, we can change it to have nothing.
When bigbadtakeover.com takes over your company, you can edit siteroot to have http://www.bigbadtakeover.com/myproductionsite/

Relative URLs

Now for the ideal situation. If you have followed the siteroot method above, edit it so that the code is the one below.

<dtml-in "PARENTS[1:]">../</dtml-in>

Test it out by browsing to these URLs in your Zope installation.

How is that? So, whereever you call siteroot from, you get the relative path! Every Zope object (other than the root) has a parent object. This hierarchy is stored in the PARENTS array. So, all you are doing here is going up the PARENTS hierarchy, and for every parent print ../ . Since we don't need the current folder, we start from the second item in the array, by calling it as PARENTS[1:] instead of PARENTS[].

Let us say the bidbadtakeover.com forces you to move your site one level below. How do you modify your siteroot? First, you need to move over everything to a subfolder. Then you can use PARENTS[2:] as the array, in the code.


  1. Zope.org - see Zope content manager's guide.