Re: [Templates] relative paths vs absolute

[prev] [thread] [next] [Date index for 2005/06/01]

From: Andy Wardley
Subject: Re: [Templates] relative paths vs absolute
Date: 18:37 on 01 Jun 2005
Steve Kelem wrote:
> Is there a way to get TT to generate relative paths in the links instead 
> of absolute ones?

Yep, you just need to figure out how many '..'s you need to get back to 
the root of the web site, then use that as a root url which is prefixed
onto all the URLs you use.

Here's the one-liner:

  rooturl = template.name.replace('\w+\.\w+$', '').replace('\w+', '..')

Included below is the more complete version with comments and debugging
messages.  This is my lib/config/root template which gets called early
on in the pre-process phase.  It allows you to pre-define a rooturl 
variable (when you have an absolute root you want to use).  If you 
don't define it, then it generates the relative one.

[%  # This configuration template determines the URL for the root
    # of the web site from the current page template.
    #
    # The 'rooturl' value can be hard-coded to a particular value
    # (e.g. http://example.wardley.org/) in the etc/ttree.cfg
    # configuration file.  Otherwise we generate a relative path to
    # the root (e.g. ../../) based on the location of the current page
    # template.
    # 
    # For example, if the current template is "foo/bar/baz.html" 
    # then we strip off the last part of the path which indicates 
    # the file name, "baz.html" to leave "foo/bar/".  Then we convert
    # each remaining part of the path to '..', resulting in '../../'
    # This rooturl variable can then be prefixed to any sitewide url 
    # to get the correct path.  For example, "${rooturl}images/foo.gif"
    # becomes "../../images/foo.gif"

    IF rooturl;
       IF debug.sitemap;
          "    # using hard-coded rooturl: $rooturl\n" | stderr;
       END;
    ELSE;
       rooturl = template.name;
       rooturl = rooturl.replace('\w+\.\w+$', '');
       rooturl = rooturl.replace('\w+', '..');

       IF debug.sitemap;
          "    # relative rooturl for $template.name is $rooturl\n" | stderr;
       END;
    END;

-%]

HTH
A


_______________________________________________
templates mailing list
templates@xxxxxxxxxxxxxxxx.xxx
http://lists.template-toolkit.org/mailman/listinfo/templates

[Templates] relative paths vs absolute
Steve Kelem 15:42 on 01 Jun 2005

[Templates] Re: relative paths vs absolute
Johan Vromans 17:58 on 01 Jun 2005

Re: [Templates] relative paths vs absolute
Andy Wardley 18:37 on 01 Jun 2005

Re: [Templates] relative paths vs absolute
Steve Kelem 23:41 on 01 Jun 2005

Generated at 20:13 on 05 Jun 2005 by mariachi v0.52